【发布时间】:2014-08-22 12:12:12
【问题描述】:
我已尝试在 SO 中搜索可能与我的情况相似的解决方案和问题。
我有 2 个对象集合:
public class BRSDocument
{
public string IdentifierValue { get; set;}
}
public class BRSMetadata
{
public string Value { get; set;}
}
我从我的数据层填写列表:
List<BRSDocument> colBRSDocuments = Common.Instance.GetBRSDocuments();
List<BRSMetadata> colBRSMetadata = Common.Instance.GetMessageBRSMetadata();
我现在想在 colBRSDocuments 中找到一个对象,其中 x.IdentifierValue 等于 colBRSMetadata y.Value 中的一个对象。我只需要找到与 BRSMetadata 对象中的值匹配的 BRSDocument。
我使用了一个普通的 foreach 循环和一个简单的 linq 搜索来查找数据并在找到值时中断。我想知道是否可以完全使用 linq 完成搜索?
foreach (var item in colBRSMetadata)
{
BRSDocument res = colBRSDocuments.FirstOrDefault(x => x.IdentifierValue == item.Value);
if (res != null)
{
//Do work
break;
}
}
希望你们中的一些人能把我推向正确的方向......
【问题讨论】:
标签: c# linq collections linq-to-objects