在一个 c# 项目中,我有两个集合,我正在尝试使用 lambda 查询它们以返回一个值
伪代码
Collection1
alertID
SavedItemID
SavedNameCollection2
SavedItemID
alertID
AlertType
DateCreated
var name = Collection2.where(c => c.savedItemID == Collection1.SavedItemID) return SavedName Property value from collection 1.
基本上在初始查询中,我正在点击 collection2 我想从 collection2 向上导航到 collection 1 以返回保存的名称字段。我从未尝试通过树向上导航来检索另一个对象。谁能就如何构建此表达式提供一些建议?
谢谢
请您参考如下方法:
您需要使用 Join然后 project一个anonymous type由您关心的字段组成。
var result = from c2 in Collection2
join c1 in Collection1 on c2.SavedItemID equals c1.SavedItemID
select new { c2.SavedItemID, c1.SavedName };
这相当于
var result = Collection2.Join(
Collection1,
c2 => c2.SavedItemId,
c1 => c1.SavedItemId,
(c2, c1) => new { c2.SavedItemID, c1.SavedName });
