如何将WorkItemCollection转换为List

时间:2014-02-17 06:31:23

标签: c# linq list collections tfs

我想将WorkItemCollection转换为List<WorkItem>,以便我可以将其进一步转换为dictionary。这是迄今为止的代码:

var testItemCollectionList = new List<WorkItem>();
WorkItemCollection testItemCollection;
Query query = new Query(project.Store, "Select [Title] From WorkItems", testResults.Select(item => item.TargetId).ToArray());
var car = query.BeginQuery();
testItemCollection = query.EndQuery(car);
testItemCollectionList = ???;
var testItemMapQuery = testItemCollectionList.ToDictionary(w => w, createItemFromQuery);

2 个答案:

答案 0 :(得分:7)

由于WorkItemCollection通过IEnumerable实施ReadOnlyList,您应该可以使用.Cast<WorkItem>(),然后直接转换为Dictionary

var testItemMapQuery = testItemCollection.Cast<WorkItem>()
                                         .ToDictionary(w => w, createItemFromQuery);

答案 1 :(得分:6)

testItemCollectionList = (from WorkItem mItem in testItemCollection select mItem).ToList();