C# Linq 查询连接两个列表并将结果分配给新列表
第一个模型:
public class Group
{
public string Name { get; set; }
public string Sid { get; set; }
}
第二个模型:
public class GuidelinesWorkTeam
{
public int Id { get; set; }
public string Name { get; set; }
public string Sid { get; set; }
public bool Active { get; set; }
}
我有这个方法:
public IEnumerable<GuidelinesWorkTeam> GetWorkTeamsWhereChangeName(
IEnumerable<Group> adGroups,
IEnumerable<GuidelinesWorkTeam> gwtItems)
{
return gwtItems.Where(gwtItem => adGroups.Any(adGroup => gwtItem.Sid == adGroup.Sid && gwtItem.Name != adGroup.Name));
}
adGroups input
Object | Name | Sid |
---|---|---|
[0] | WorkTeam1 | Sid1 |
[1] | WorkTeam3 | Sid2 |
gwtItems input
Object | Id | Name | Sid | Active |
---|---|---|---|---|
[0] | 1 | WorkTeam1 | Sid1 | true |
[1] | 2 | WorkTeam2 | Sid2 | true |
所需结果
对象 | Id | 名称 | Sid | Active |
---|---|---|---|---|
[0] | 2 | WorkTeam3 | Sid2 | true |
演示是 这里
我需要返回一个 IEnumerable
(或其他 IEnumerable,因为我只需要adGroup.Name
和 gwtItem.Id
) 元素(如果满足上述条件),但将 gwtItem.Name
替换为adGroup.Name
。
我怎样才能做到这一点?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据我对您问题的理解,我想提出两种不同的方法来解决该问题:
adGroups
中的项目,然后再将adGroups
项目与gwtItems 关联
项目gwtItems
中的项目,然后)将gwtItems
项目与adGroups
项目关联起来,具体取决于在哪个变量(
adGroups
或gwtItems
)从最初的过滤中获益最多,您可以选择最适合您的用例的方法。过滤
adGroups
项目,然后将它们与gwtItems
项目相关联这里,
adGroups
中的所有适用项目均按Sid
在每个基于Sid
的分组与基于 gwtItem 的Sid
值的gwtItems
中的项目相关联之前。将adGroups
分组和gwtItems
与相等的Sid
(以及不相等的Name
)关联后,由于的初始过滤>adGroups
)使用.Join()
,我们选择每个关联的 adGroup 和 gwtItem 对的adGroup.Name
和gwtItem.Id 在
tuple
并使用SelectMany()
来展平集合(来自IEnumerable>>
到IEnumerable< * >
)。将
gwtItems
项目与adGroups
项目关联(首先过滤gwtItems
后)这里,来自
gwtItems
的项目是首先根据相应类的Sid
属性,使用.Join()
与adGroups
中的项目关联,然后再使用包含相关信息的元组从返回.Join()
操作;然后,仅选择实际上具有不同名称的关联项并将其转换为更简单的元组。如果您的原始过滤可能会从
gwtItems
中过滤掉许多项目,则可能应该包含该过滤;但不一定要获得所需的结果。Based on my understanding of your question, I would like to propose two different approaches to solve it:
adGroups
before associating theadGroups
items with thegwtItems
itemsgwtItems
as you have already done, and then) associating thegwtItems
items with theadGroups
itemsDepending on which variable (
adGroups
orgwtItems
) benefits most from being filtered initially, you may choose which approach fits your use case best.Filtering the
adGroups
items, then associating them with thegwtItems
itemsHere, all applicable items from
adGroups
are grouped bySid
before eachSid
-based grouping is associated with items fromgwtItems
based on the gwtItem'sSid
value. After associatingadGroups
groupings andgwtItems
with equalSid
(and unequalName
, due to the initial filtering ofadGroups
) using.Join()
, we select each associated adGroup and gwtItem pair'sadGroup.Name
andgwtItem.Id
in atuple
and useSelectMany()
to flatten the collection (from aIEnumerable<IEnumerable< * >>
to aIEnumerable< * >
).Associating the
gwtItems
items with theadGroups
items (after filtering thegwtItems
first)Here, the items from
gwtItems
are first associated with the items fromadGroups
based on theSid
property from the respective classes, using.Join()
before a tuple containing relevant information is returned from the.Join()
operation; then, only the associated items that actually have different names are selected and converted to a simpler tuple.Your original filtering should possibly be included if it is probable that it will filter out many items from
gwtItems
; but it shouldn't be necessary to obtain the desired result.