我有下面的代碼來做我想做的事但我想知道是否有一種方法可以直接從linq查詢中做同樣的事情。如何從linq查詢中創建一個List <Dictionary <string,string >>列表
XElement xmlData = XElement.Parse(items.Xml);
var itemsNotSynched =
(from a in xmlData.Descendants(XName.Get("row", "#RowsetSchema"))
group a by new
{
employeeID = (string)a.Attribute("ows_EmployeeID"),
courseID = (string)a.Attribute("ows_CourseID"),
title = (string)a.Attribute("ows_LinkTitle")
} into ex
select new
{
ex.Key.title,
ex.Key.courseID,
ex.Key.employeeID
} into eb
select eb).ToArray();
List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
foreach(var item in itemsNotSynched)
{
Dictionary<string, string> itm = new Dictionary<string, string>();
itm.Add("employeeID", item.employeeID.ToString());
if(item.courseID != null)
{
itm.Add("courseID", item.courseID.ToString());
}
itm.Add("title", item.title.ToString());
list.Add(itm);
}
由於提前,
-EC-
編輯1.
我設法得到我想要使用來自SLaks的建議......我給它另一射擊在.Distinct()
XElement xmlData = XElement.Parse(items.Xml);
List<Dictionary<string,string>> itemsNotSynched =
(from a in xmlData.Descendants(XName.Get("row", "#RowsetSchema"))
group a by new
{
employeeID = (string)a.Attribute("ows_EmployeeID"),
courseID = (string)a.Attribute("ows_CourseID"),
title = (string)a.Attribute("ows_LinkTitle")
} into g
select new Dictionary<string, string>
{
{"employeeID", g.Key.employeeID },
{"courseID", g.Key.courseID },
{"title", g.Key.title}
} into f
select f).ToList();
您應該使用'.Distinct()'而不是'group by'。 – SLaks 2012-03-13 15:27:13
我似乎無法讓.Distinct()以我想要的方式工作,但它總是返回更多的數據(122條記錄),因爲分組只能返回15個數據。另外我想提一下,結果集來自SharePoint列表,其中有一些額外的列,我似乎無法擺脫。儘管我在CAML查詢中指定了我想要的列。 – user1266781 2012-03-13 18:28:28
您需要在匿名類型集合(在Select()後面)調用'Distinct()'。 – SLaks 2012-03-13 18:29:28