여러 열로 그룹화
LINQ에서 GroupBy 다중 열을 수행하는 방법
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();
Group By Multiple Columns의 경우 대신 이것을 시도하십시오 ...
GroupBy(x=> new { x.Column1, x.Column2 }, (key, group) => new
{
Key1 = key.Column1,
Key2 = key.Column2,
Result = group.ToList()
});
같은 방법으로 Column3, Column4 등을 추가 할 수 있습니다.
C # 7부터 값 튜플을 사용할 수도 있습니다.
group x by (x.Column1, x.Column2)
또는
.GroupBy(x => (x.Column1, x.Column2))
강력한 형식의 그룹화를 위해 Tuple <>을 사용할 수도 있습니다.
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);
Tuples
및 사용하는 C # 7.1 이상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.Col, x.Col}으로 x 그룹화
A thing to note is that you need to send in an object for Lambda expressions and can't use an instance for a class.
Example:
public class Key
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
}
This will compile but will generate one key per cycle.
var groupedCycles = cycles.GroupBy(x => new Key
{
Prop1 = x.Column1,
Prop2 = x.Column2
})
If you wan't to name the key properties and then retreive them you can do it like this instead. This will GroupBy
correctly and give you the key properties.
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;
}
참고URL : https://stackoverflow.com/questions/847066/group-by-multiple-columns
'code' 카테고리의 다른 글
이벤트를 발생시킨 요소의 ID 가져 오기 (0) | 2020.09.28 |
---|---|
Node.js를 사용하여 현재 스크립트의 경로를 어떻게 얻습니까? (0) | 2020.09.28 |
Bootstrap 열을 모두 같은 높이로 만들려면 어떻게해야합니까? (0) | 2020.09.28 |
NSString 값을 NSData로 어떻게 변환합니까? (0) | 2020.09.28 |
파이썬에서 줄 수를 저렴하게 얻는 방법은 무엇입니까? (0) | 2020.09.28 |