【发布时间】:2016-09-14 20:53:18
【问题描述】:
我想查询一个对象数组“sortedData”,其中每个对象都有两个值(ItemId,Sort),用于特定的ItemId并设置“Sort”值。如下所示,但这不是正确的 linq 语法。
var sortedData = db.Fetch<object>("SELECT ItemId, Sort FROM CollectionItems WHERE CollectionId = @0", collectionId);
dataWithSort = db.Fetch<OrganizationForExportWithSort>(TpShared.DAL.StoredProcedures.GetOrganizationsForTargetListUI(clientId, organizationIdList));
foreach(OrganizationForExportWithSort export in dataWithSort)
{
export.Sort = sortedData.Select("Sort").Where(sortedData.ItemId == export.Id);
}
【问题讨论】:
-
无论何时使用 Linq,您都会收到一个数据收集作为回报。 Where 语句允许您细化结果集合中的元素,而 Select 允许您对每个元素执行操作。我强烈建议你在尝试之前先在 Linq 中做一些简单的教程。
-
我也试过 export.Sort = from d in sortedData where d.ItemId == export.Id select d.Sort;但这也不起作用,因为它没有找到 d.ItemId
-
您提供的代码示例无法编译。