题
我该如何在linq中进行多个列
与SQL类似的东西:
SELECT * FROM <TableName> GROUP BY <Column1>,<Column2>
如何将其转换为Linq:
QuantityBreakdown
(
MaterialID int,
ProductID int,
Quantity float
)
INSERT INTO @QuantityBreakdown (MaterialID, ProductID, Quantity)
SELECT MaterialID, ProductID, SUM(Quantity)
FROM @Transactions
GROUP BY MaterialID, ProductID
解决方案
使用匿名类型。
例如
group x by new { x.Column1, x.Column2 }
其他提示
程序样本
.GroupBy(x => new { x.Column1, x.Column2 })
好的,这是:
var query = (from t in Transactions
group t by new {t.MaterialID, t.ProductID}
into grp
select new
{
grp.Key.MaterialID,
grp.Key.ProductID,
Quantity = grp.Sum(t => t.Quantity)
}).ToList();
对于多列组,请尝试...
GroupBy(x=> new { x.Column1, x.Column2 }, (key, group) => new
{
Key1 = key.Column1,
Key2 = key.Column2,
Result = group.ToList()
});
相同的方式可以添加列3,第4列等。
由于C#7,您也可以使用值元组:
group x by (x.Column1, x.Column2)
或者
.GroupBy(x => (x.Column1, x.Column2))
您也可以使用元组<>进行强大的分组。
from grouping in list.GroupBy(x => new Tuple<string,string,string>(x.Person.LastName,x.Person.FirstName,x.Person.MiddleName))
select new SummaryItem
{
LastName = grouping.Key.Item1,
FirstName = grouping.Key.Item2,
MiddleName = grouping.Key.Item3,
DayCount = grouping.Count(),
AmountBilled = grouping.Sum(x => x.Rate),
}
尽管这个问题是按类属性询问组,但是如果要通过多个列对ADO对象进行分组(例如DataTable),则必须将“新”项目分配给变量:
EnumerableRowCollection<DataRow> ClientProfiles = CurrentProfiles.AsEnumerable()
.Where(x => CheckProfileTypes.Contains(x.Field<object>(ProfileTypeField).ToString()));
// do other stuff, then check for dups...
var Dups = ClientProfiles.AsParallel()
.GroupBy(x => new { InterfaceID = x.Field<object>(InterfaceField).ToString(), ProfileType = x.Field<object>(ProfileTypeField).ToString() })
.Where(z => z.Count() > 1)
.Select(z => z);
C#7.1或更大 使用 Tuples
和 Inferred tuple element names
:
// declarative query syntax
var result =
from x in table
group x by (x.Column1, x.Column2) into g
select (g.Key.Column1, g.Key.Column2, QuantitySum: g.Sum(x => x.Quantity));
// or method syntax
var result2 = table.GroupBy(x => (x.Column1, x.Column2))
.Select(g => (g.Key.Column1, g.Key.Column2, QuantitySum: g.Sum(x => x.Quantity)));
C#3或更大 使用 anonymous types
:
// declarative query syntax
var result3 =
from x in table
group x by new { x.Column1, x.Column2 } into g
select new { g.Key.Column1, g.Key.Column2, QuantitySum = g.Sum(x => x.Quantity) };
// or method syntax
var result4 = table.GroupBy(x => new { x.Column1, x.Column2 })
.Select(g =>
new { g.Key.Column1, g.Key.Column2 , QuantitySum= g.Sum(x => x.Quantity) });
var Results= query.GroupBy(f => new { /* add members here */ });
.GroupBy(x => (x.MaterialID, x.ProductID))
.GroupBy(x => x.Column1 + " " + x.Column2)
X组由new {X.Col,X.Col}组
要注意的一件事是,您需要在lambda表达式中发送对象,并且不能为类使用实例。
例子:
public class Key
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
}
这将编译,但会产生 每个周期的一个关键.
var groupedCycles = cycles.GroupBy(x => new Key
{
Prop1 = x.Column1,
Prop2 = x.Column2
})
如果您不愿命名密钥属性,然后重新命名,则可以这样做。这将 GroupBy
正确地为您提供关键属性。
var groupedCycles = cycles.GroupBy(x => new
{
Prop1 = x.Column1,
Prop2= x.Column2
})
foreach (var groupedCycle in groupedCycles)
{
var key = new Key();
key.Prop1 = groupedCycle.Key.Prop1;
key.Prop2 = groupedCycle.Key.Prop2;
}
不隶属于 StackOverflow