一、遍历DataTable
已存在一个DataTable,其中字段名为
ID(int),Name(string),Company(string),CreatedDate(DateTime)
对应此表的class为:
public class ClientStruct
{
public string ID { get; set;};
public string Name {get; set;};
public string Company { get; set;};
public string CreatedDate { get; set;};
}
遍历DataTable,取出所有的ID
List<string> lstID = (from a in dtTable.AsEnumerable() select a.Field<Int32>("ID").ToString()).ToList<string>();
if (dtTable != null && dtTable.Rows.Count > 0)
{
List<ClientStruct> list = (from a in dtTable.AsEnumerable()
orderby a.Field<string>("Company")
select new ClientStruct
{ ID = a.Field<Int32>("ID").ToString(),
Name = a.Field<string>("Name"),
Company = a.Field<string>("Company"),
CreateDate = a.Field<DateTime>("CreatedDate").ToString("yyyy-MM-dd") }).ToList<ClientStruct>();
}
二、遍历DataTable,并将上面的List结果存储到Dictionary中:
希望得到Dictionary<string, ClientStruct>(),string为ClientStruct中的Company
Dictionary<string, ClientStruct> dictionary = list.ToDictionary(p => p.Company);
三、遍历DataTable并将其中两个字段的内容存储到Dictionary中:
{
var dic = (from p in dtTable.AsEnumerable(0 orderby p.Filed<string>("Company")
select new
{
myKey = p.Field<string>("Company"),
myValue = p.Filed<DateTime)("CreateDate").ToString("yyyy-MM-dd")
} ) .AsEnumberable().ToDicitionary(k=>k. myKey, v=>v. myValue);
}
Dictionary<string, string> dic = dtTable.AsEnumerable().ToDictionary(v => v["Company"].ToString(), t => t["CreateDate"].ToString("yyyy-MM-dd"));
public class ProjectStruct
{
public string ProjectID { get; set;}
public string ProjectName { get; set; }
public int Status { get; set; }
}
Dictionary<string, ProjectStruct> lstRet = new Dictionary<string, ProjectStruct>();
//dtTable为已经从数据库取得的结果
lstRet = dtTable.AsEnumerable().ToDictionary(v => v["Name"].ToString(),
t => new ProjectStruct
{
PM = t["Manager"].ToString(),
ProjectID = t["ID"].ToString(),
ProjectName = t["Name"].ToString(),
Status = Public.CastInt32(t["Status"])
});
四、选择特定数据:
Dictionary<string, int> dic = new Dictionary<string,int>();
dic.Add("test1",1);
dic.Add("test2", 2);
return (from p in dic where p.value > 0 select new {
mykey = p.Key, myvalue = p.Value
}).ToDictionary(k=>k.mykey, v=>v.myvalue);
五、Dictionary之求和:
(1)创建泛型字典students(类型为Dictionary<int,Student1>),并添加4个Student1类型的元素,元素的键值分别为1~4。
(2)使用LINQ查询泛型字典students中的所有元素,并按照元素的总分的升序排序。查询结果保存在values变量中。
(3)把查询结果(学生姓名及总成绩)输出到Web表单中。
具体实现代码如下:
private void DictionaryQuery()
{
StringBuilder str = new StringBuilder("");
//构建数据源
Dictionary<int, Student1> students = new Dictionary<int, Student1>();
students.Add(1, new Student1 { Name = "Svetlana", Scores = new int[] { 98, 92, 81, 60 } });
students.Add(2, new Student1 { Name = "Claire", Scores = new int[] { 75, 84, 91, 39 }});
students.Add(3, new Student1 { Name = "Sven", Scores = new int[] { 88, 94, 65, 91 }});
students.Add(4, new Student1 { Name = "Cesar", Scores = new int[] { 97, 89, 85, 82 }});
//查询泛型字典
var values = from u in students let temp = u.Value.Scores.Sum()
orderby temp
select new { name = u.Value.Name, totalscore = temp };
//显示查询结果
foreach (var v in values)
{
str.AppendFormat("学生姓名:{0},总分是:{1}</br>", v.name, v.totalscore);
}
//把查询结果显示于Web表单中
Label1.Text = str.ToString();
}
注意到,本例中在查询中利用了聚合查询之一,即Sum操作,求出当前学生的总分。
本例的输出结果如图所示。