我有 2 個不同的通用串列,其中包含一些我想用于比較的通用欄位,并從其他串列回傳不匹配的行,反之亦然。
第一個串列:列出 shippingCharges 第二個串列:列出 shippingCharges
我想比較這兩個串列,其中“家庭”在第二個串列中不匹配,并生成一個新的差異串列,并希望在下一步中重復反之亦然,但以下查詢在存在差異時沒有回傳任何內容:
var list0 = shippingCharges.Where(item => freightCharges.All(f => item.Buid == f.BUID && item.Catalog == (f.Catalog != null ? f.Catalog : null)
&& item.Productline == (f.ProductLine != null ? f.ProductLine : null)
&& item.Brand == (f.Brand != null ? f.Brand : null) && item.Family != (f.Family != null ? f.Family : null))).ToList();
有一些 null 的可能性,所以我也處理了它們。
我想將一個串列的每個串列項與另一個進行比較。我可以使用 foreach 回圈,但認為在 Linq 中會有相同的東西。
uj5u.com熱心網友回復:
我們以兩個類為例:
public class ShippingCharges
{
public string ProductLine { get; set; }
public int Family { get; set; }
}
public class FreightCharges
{
public string Brand { get; set; }
public int? Family { get; set; }
}
我們添加一些值:
var ListA = new List<ShippingCharges>() {
new ShippingCharges()
{
ProductLine = "1",
Family = 1
},
new ShippingCharges()
{
ProductLine = "1",
Family = 2
},
};
var ListB = new List<FreightCharges>(){
new FreightCharges()
{
Brand = "2",
Family = 2
},
new FreightCharges()
{
Brand = "3",
Family = 3
},
};
添加一些 LINQ 擴展:
public static class LinqExtensions
{
public static IEnumerable<TSource> Except<TSource, VSource>(this IEnumerable<TSource> first, IEnumerable<VSource> second, Func<TSource, VSource, bool> comparer)
{
return first.Where(x => second.Count(y => comparer(x, y)) == 0);
}
public static IEnumerable<TSource> Contains<TSource, VSource>(this IEnumerable<TSource> first, IEnumerable<VSource> second, Func<TSource, VSource, bool> comparer)
{
return first.Where(x => second.FirstOrDefault(y => comparer(x, y)) != null);
}
public static IEnumerable<TSource> Intersect<TSource, VSource>(this IEnumerable<TSource> first, IEnumerable<VSource> second, Func<TSource, VSource, bool> comparer)
{
return first.Where(x => second.Count(y => comparer(x, y)) == 1);
}
}
獲取串列 A 中不在串列 b 中的所有元素:
var newData = ListA.Except(ListB, (a,b) => a.Family == b.Family);
簡短說明:f.Brand != null ? f.Brand : null等于f.Brand ?? null等于f.Brand
uj5u.com熱心網友回復:
如果您將更頻繁地將它用于不同的類,我的建議是為此創建一個通用擴展方法,這樣您就可以像使用標準 LINQ 方法一樣重用它。如果您不熟悉擴展方法,請閱讀擴展方法揭秘。
所以我們必須輸入不同型別的序列。序列 1 中的物件具有屬性 X,序列中的物件具有屬性 Y。您可以檢查屬性 X 和 Y 的值之間的相等性。
要求:回傳序列 1 中的所有物件,其屬性 X 的值不等于序列 2 中物件的任何 Y 值。反之亦然。
因此,讓我們首先為一側制作一個程式。然后我們重復使用它,Concat反之亦然
public static WhereNotMatched<Touter, Tinner, TKey>(
this IEnumerable<Touter> outer,
IEnumerable<Tinner> inner,
Func<Touter,TKey> outerKeySelector,
Func<TInner,TKey> innerKeySelector)
{
return WhereNotMatched(outer, inner, outerKeySelector, innerKeySelector, null);
{
public static IEnumerable<Touter> WhereNotMatched<Touter, Tinner, TKey>(
this IEnumerable<Touter> outer,
IEnumerable<Tinner> inner,
Func<Touter,TKey> outerKeySelector,
Func<TInner,TKey> innerKeySelector
IEqualityComparer<TKey> comparer)
{
// if no comparer provided, use the default comparer
if (comparer == null) comparer = EqualityComparer<TKey>.Default;
// TODO: check outer != null, inner != null, etc.
// extract all inner keys, for efficient lookup put them in a HashSet<TKey>
HashSet<TKey> innerKeys = new HashSet(inner.Select(i => innerKeySelector(i)), comparer);
// return all outer that have no corresponding value in the HashSet
return outer.Where(o => !innerKeys.Contains(innerKeySelector(o));
}
用法:
假設在一個訂購系統中,您有 Products、Orders 和 OrderLines。每個 OrderLine 都有一個外鍵 ProductId。此 ProductId 是指此 OrderLine 中提到的產品。每個訂單包含零個或多個訂單行。
IEnumerable<Order> orders = ...
IEnumerable<Product> products = ...
// Get all Products that have never been ordered yet
IEnumerable<OrderLine> orderLines = orders.SelectMany(order => order.OrderLines);
IEnumerable<Product> neverOrderedProducts = products.WhereNotMatched(orderLines,
product => product.Id,
orderLine => orderLine.ProductId);
以下是您要求的更多內容:
IEnumerable<Student> maleStudents = ...
IEnumerable<Student> femaleStudents = ...
假設每個學生都有一個PromCompanion. 我們不是老式的,所以一些男學生有一個不是女學生的 PromCompanion,同樣,一些女學生有一個非男學生作為 PromCompanion。找到所有這些學生。
IEnumerable<Student> result = maleStudents.WhereNotMatched(femaleStudents,
man => man.PromCompanion,
woman => woman.PromCompanion)
// concat with vice-versa
.Concat(femaleStudents.WhereNotMatched(maleStudents,
woman => woman.PromCompanion,
man => man.PromCompanion));
IEnumreable<Student> uncompaniedStudents = maleStudents.WhereNon
uj5u.com熱心網友回復:
您將必須覆寫正在比較的模型的 Equal 方法。有關如何以及為什么這樣做的更多資訊,請參閱 MSDocs 中的此鏈接。
https://docs.microsoft.com/en-us/dotnet/api/system.object.equals?view=net-6.0
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/405930.html
標籤:
上一篇:后代ID串列-子女、孫子女等
下一篇:StringComparison.InvariantCultureIgnoreCase在LINQ查詢中使用時無法翻譯
