使用LINQ的不同类型的两个列表的区别 [英] Difference of two List with different types using LINQ
问题描述
我正在尝试通过LINQ查询获得两个列表的不同之处.但是列表的类型不同.
I am trying the get the different of two lists with a LINQ query. But the lists are not of the same type.
List<Vehicle> unsoldVehicles
List<string> ListDetailUrls
每个车辆对象都有一个名为 UrlID (字符串)的字段,而 ListDetailUrls 列表仅包含字符串. 我需要车辆列表中的所有车辆,其中字段 UrlID 与条目 ListDetailUrls 不匹配.
Each Vehicle Object has a field called UrlID (string) while the ListDetailUrls list consists only of strings. I need every Vehicle from the Vehicle List where the field UrlID does not match an entry ListDetailUrls.
到目前为止,我所做的是:
What I have done so far is :
List<Vehicle> missingVehicles = new List<Vehicle>(
from uV in unsoldVehicles
from de in ListDetailUrls
where uV.UrlID != de
select uV);
但是通过这样的查询,我的 missingVehicles 比 unsoldVehicles 还要多!
But with a query like this my missingVehicles are more items than the unsoldVehicles!
我一直在寻找一种以某种方式使用Except方法的方法,但是我只找到两个列表属于同一类型的示例.
I was looking for a way to use the Except-method somehow but I only find samples where both lists are of the same type.
推荐答案
您可以尝试这样的操作.
You could try something like this.
var missingvehicles = from uV in unsoldVehicles
where !listDetailUrls.Contains(uV.UrlID )
select uV;
这篇关于使用LINQ的不同类型的两个列表的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!