Linq-除了一个列表,另一个列表中有项目 [英] Linq - Except one list with items in another

查看:41
本文介绍了Linq-除了一个列表,另一个列表中有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我的问题很简单,但是我是linq的新手...所以我在这里很难过

I think my question is easy, but I'm a newbie in linq... So I'm having a hard time here

我的系统调用了一个称为serviceTOP的服务,该服务向我返回了itemTOP {Id, Name}的列表.

My system calls a service, called serviceTOP, that returns me a list of itemTOP {Id, Name}.

这些ItemsTOP不在我的系统中,但是用户可以选择要导入到系统中的itemTOP.

These ItemsTOP aren't in my system, but the user can choose which itemTOP to import to the system.

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

The imported ItemsTOP becomes an object Item { Id, IdTOP, Name }

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

So, when the system calls serviceTOP, before showing them to the user, I must filter the already imported items from the list.

我们去编写代码:

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

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

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

这种工作方式,但是它返回仅包含id的字符串列表.

This kind of works, but it returns a list of strings containing only the id.

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

I'd like to select both id and name of the TOP.

提前谢谢.

推荐答案

更改此内容:

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

对此:

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

这篇关于Linq-除了一个列表,另一个列表中有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆