我有一個有6列的表,每列代表一個object
,因此每個錶行都是6個對象的集合,每個對象都有一個屬性color
。LINQ to屬性的對象對象集合約束
如何選擇行中不超過4個單色對象的所有行?
例如:
obj1.color=red, obj2.color=green, obj3.color=blue, obj4.color=green, obj5.color=blue, obj6.color=green
我有一個有6列的表,每列代表一個object
,因此每個錶行都是6個對象的集合,每個對象都有一個屬性color
。LINQ to屬性的對象對象集合約束
如何選擇行中不超過4個單色對象的所有行?
例如:
obj1.color=red, obj2.color=green, obj3.color=blue, obj4.color=green, obj5.color=blue, obj6.color=green
在它構造具有的所有對象的陣列中的一行。然後按顏色分組,並檢查所有組中沒有超過4個項目。
var rows = tbl.Where(x =>
new[]
{
x.obj1,
x.obj2,
x.obj3,
x.obj4,
x.obj5,
x.obj6
}
.GroupBy(y => y.color)
.All(g => g.Count() < 4));
當我運行這個\t var rows = tbl.Where(x => new [] {xg,x.d1,x)時,我不太擅長lambda語法或LINQ。 D2,x.c1,x.c2,x.rw1,x.rw2,x.lw1,x.lw2} \t \t \t \t \t .GroupBy(Y => y.Color) \t \t \t \t \t。所有(z => z.Count()<= 4));; \t rows.Dump();我在LINQPad中遇到一個錯誤:「Constructed arrays are only for Contains。 – flux9998
and ur expression is not correct,testing it。syntax error – Alex
u can not convert type to bool – Alex
這裏是你如何能作出這樣的工作linqpad樣本:
// sample table
var table = new[] {
new {c1="blue", c2="red", c3="green"},
new {c1="blue", c2="blue", c3="blue"},
new {c1="blue", c2="red", c3="green"},
new {c1="red", c2="red", c3="red"},
};
table.Where(row => (
new[] {row.c1, row.c2, row.c3}) // transform rows to array of vals
.GroupBy(r => r) // group the vals
.Any(r => r.Count() == 3)) // filter the groups
.Dump(description: "I love LINQPad!");
到目前爲止什麼都試過?添加一些你已經試過的代碼 – Alex