LINQ泛型數據集淺談
學習LINQ時,經常會遇到LINQ泛型數據集問題,這里將介紹LINQ泛型數據集問題的解決方法。
查詢是一種從數據源檢索數據的表達式。查詢用專用查詢語言表示。隨著時間的推移,人們已經為不同類型的數據源開發了不同的語言,例如,用于關系數據庫的 SQL 和用于 XML 的 XQuery。這使應用程序開發人員必須針對所支持的每種數據源或數據格式而學習新的查詢語言。
語言集成查詢 (LINQ) 通過提供一種跨各種數據源和數據格式使用數據的一致模型,簡化了這一情況。在 LINQ 查詢中,始終會用到對象。在查詢和轉換 XML 文檔、SQL 數據庫、ADO.NET 數據集和實體、.NET Framework 集合中的數據以及具有相應的 LINQ 提供程序的任何其他源或格式的數據時,都會使用相同的基本編碼模式。
定義一個返回LINQ泛型數據集代碼:
- using System;
- using System.Collections.Generic;
- namespace BlueCube.BusinessLogic
- {
- /// <summary>
- /// Encapsulates execution result contains whether the
execution is successful and what messages the invoker will receive.- /// </summary>
- public class ExecutionResult<T>
- {
- /// <summary>
- /// True as execution is successful. False as failed.
- /// </summary>
- public bool Success
- {
- get;
- set;
- }
- private List<string> _Messages = null;
- /// <summary>
- /// Stores message list
- /// </summary>
- public List<string> Messages
- {
- get
- {
- // Initialize message list if it is null
- if (_Messages == null)
- {
- _Messages = new List<string>();
- }
- return _Messages;
- }
- set
- {
- // Clear existed message list then add new list from value
- if (_Messages != null)
- {
- _Messages.Clear();
- foreach (string message in value)
- {
- _Messages.Add(message);
- }
- }
- else
- {
- _Messages = value;
- }
- }
- }
- /// <summary>
- /// Encapsulates the value if there is any return value during execution
- /// </summary>
- public T ReturnValue
- {
- get;
- set;
- }
- }
- }
以上介紹定義一個返回LINQ泛型數據集。
【編輯推薦】