[英]Querying 2 Sets of Complex-Objects Using Linq
I have two lists comprised of different complex-objects, and each one is from 2 separate data-sources. 我有两个包含不同复杂对象的列表,每个列表都来自2个单独的数据源。 One list may-or-may-not contain records. 一个列表可能包含也可能不包含记录。 When any records exist in the "optional" list I need the "normal" list to be further-filtered. 当“可选”列表中存在任何记录时,我需要对“正常”列表进行进一步过滤。
Unfortunately, I can only find very simple examples here and online, which is why I am asking this question. 不幸的是,我只能在这里和在线上找到非常简单的示例,这就是为什么我问这个问题。
The Pseudo-Logic Goes Like This: 伪逻辑如下所示:
When QuickFindMaterial
records exist, get all DataSource
records where query.Name
is in the QuickFindMaterial.Material
collection. 当存在QuickFindMaterial
记录时,获取所有DataSource
记录,其中query.Name
在QuickFindMaterial.Material
集合中。 If no QuickFindMaterial
records exist do not affect the final result. 如果不存在QuickFindMaterial
记录,则不会影响最终结果。 Lastly, select all distinct DataSource
records. 最后,选择所有不同的DataSource
记录。
The Classes Looks Like: 这些类看起来像:
public class QuickFindMaterial
{
public string SiteId { get; set; }
public string Material { get; set; }
}
The Code Looks Like: 该代码看起来像:
I have commented-out my failed WHERE
logic below 我在下面注释掉了我失败的WHERE
逻辑
var dataSource = DocumentCollectionService.ListQuickFind();
var quickFindMaterial = ListMaterialBySiteID(customerSiteId);
var distinct = (from query in dataSource
select new
{
ID = query.DocumentID,
Library = query.DocumentLibrary,
ModifiedDate = query.DocumentModifiedDate,
Name = query.DocumentName,
Title = query.DocumentTitle,
Type = query.DocumentType,
Url = query.DocumentUrl,
})
//.Where(x => x.Name.Contains(quickFindMaterial.SelectMany(q => q.Material)))
//.Where(x => quickFindMaterial.Contains(x.Name))
.Distinct();
我认为这是您想要的:
.Where(x => !quickFindMaterial.Any() || quickFindMaterial.Any(y => x.Name == y.Material))
You could join on Name -> Material 您可以加入Name-> Material
Example: 例:
var distinct = (from query in dataSource
join foo in quickFindMaterial on query.Name equals foo.Material
select new
{
ID = query.DocumentID,
Library = query.DocumentLibrary,
ModifiedDate = query.DocumentModifiedDate,
Name = query.DocumentName,
Title = query.DocumentTitle,
Type = query.DocumentType,
Url = query.DocumentUrl,
}).Distinct();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.