gpt4 book ai didi

c# - Linq - 除了一个列表和另一个列表中的项目

转载 作者:太空狗 更新时间:2023-10-29 21:24:44 26 4
gpt4 key购买 nike

我觉得我的问题很简单,但我是 linq 的新手......所以我在这里遇到了困难

我的系统调用一个名为 serviceTOP 的服务,该服务返回一个 itemTOP {Id, Name} 列表。

这些ItemsTOP不在我的系统中,但是用户可以选择将哪个itemTOP导入系统。

导入的ItemsTOP成为对象Item { Id, IdTOP, Name }

因此,当系统调用 serviceTOP 时,在将它们显示给用户之前,我必须从列表中过滤已经导入的项目。

让我们开始编码:

IList<ItemsTOP> listTOP = new ServiceTOP().GetItemsTOP();

IList<Items> list = new WCFServiceClient().GetItems();

var filteredListTOP = listTOP.Select( i => i.Id ).Except( i => i.IdTOP );

这种方法可行,但它返回一个仅包含 id 的字符串列表。

我想同时选择 TOP 的 ID 和名称。

最佳答案

改变这个:

listTOP.Select(i => i.Id )
.Except( i => i.IdTOP );

对此:

listTOP.Select(i => new { ID = i.id, Name = i.Name} )
.Except( i => i.IdTOP );

关于c# - Linq - 除了一个列表和另一个列表中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5625689/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com