0

我有一个名为 Content 的类,其中包含一些属性

属性之一是 CultureCode。

列表 A 包含所有“en”内容类 列表 B 包含所有“en-GB”内容类

我想合并它们,以便:我们只在最终列表中获得“en”内容,但只要在 en-GB 中存在匹配条件,即将该项目包含在最终列表中而不是 en 中。

因此,如果:

清单 A

en - 1
en - 2
en - 3
en - 4
en - 5

清单 B

en-GB - 2
en-GB - 4
en-GB - 6

然后

混合列表

en - 1
en-GB - 2
en - 3
en-GB - 4
en - 5

我尝试过这样的事情:

IEnumerable<Content> mixed = from x in listA
join y in listB on new {x.Property1, x.Property2, x.Property3} equals
    new {y.Property1, y.Property2, y.Property3} into g
from o in g.DefaultIfEmpty(new Content()
{
    Id = x.Id,
    CultureCode = x.CultureCode,
    Property1 = x.Property1,...
})
where
(
    ...
)
select new Content()
{
    Id = o.Id,
    CultureCode = o.CultureCode,
    Property1 = o.Property1,
    Property2 = o.Property2,
    Property3 = o.Property3,
};

并且有多种变体,但结果永远不会完全正确。

有任何想法吗?

4

1 回答 1

1

类似这样的事情会做到这一点:

var result = (from a in listA
              join b in listB on a.Property1 equals b.Property1 into g
              from j in g.DefaultIfEmpty()
              select new Content() { 
                   // Favour listA's culture code
                   CultureCode = j == null ? a.CultureCode : j.CultureCode, 
                   Property1 = a.Property1 
              });

和一个活生生的例子来演示:http ://rextester.com/AAWQ92029

于 2014-01-22T11:35:51.633 回答