LINQでGroupBy Multiple Columnsを実行する方法
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() 
});
同じ方法でColumn3、Column4などを追加できます。
Resultすべての列にリンクされているすべてのデータセットを含みます。どうもありがとう! - j00hi
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),
}
                    
                                            
この質問では、クラスプロパティによるグループ化について質問していますが、(DataTableのように)ADOオブジェクトに対して複数列でグループ化したい場合は、「新しい」アイテムを変数に割り当てる必要があります。
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);
                    
                                            
.GroupBy(x => x.Column1 + " " + x.Column2)
                    
                                            Linq.Enumerable.Aggregate()これにより、動的な数のプロパティによるグループ化も可能になります。propertyValues.Aggregate((current, next) => current + " " + next)。 - Kai Hartmann
xをnew {x.Col、x.Col}でグループ化
.GroupBy(x => (x.MaterialID, x.ProductID))
注意しなければならないのは、Lambda式にはオブジェクトを送信する必要があり、クラスにはインスタンスを使用できないということです。
例:
public class Key
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
}
これはコンパイルしますが生成しますサイクルごとに1つのキー。
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;
}