2016-07-10 3 views
2

私のコードでは、Invoicesの集約番号InvoiceLineの合計と、それぞれInvoiceに関連付けられたTracksのリストを取得したいと考えています。グループ化された要素のSelectMany

var screenset = 
    from invs in context.Invoices 
    join lines in context.InvoiceLines on invs.InvoiceId equals lines.InvoiceId 
    join tracks in context.Tracks on lines.TrackId equals tracks.TrackId 
    group new { invs, lines, tracks } 
    by new 
    { 
     invs.InvoiceId, 
     invs.InvoiceDate, 
     invs.CustomerId, 
     invs.Customer.LastName, 
     invs.Customer.FirstName 
    } into grp 
    select new 
    { 
     InvoiceId = grp.Key.InvoiceId, 
     InvoiceDate = grp.Key.InvoiceDate, 
     CustomerId = grp.Key.CustomerId, 
     CustomerLastName = grp.Key.LastName, 
     CustomerFirstName = grp.Key.FirstName, 
     CustomerFullName = grp.Key.LastName + ", " + grp.Key.FirstName, 
     TotalQty = grp.Sum(l => l.lines.Quantity), 
     TotalPrice = grp.Sum(l => l.lines.UnitPrice), 
     Tracks = grp.SelectMany(t => t.tracks) 
    }; 

しかし、最後の行で、私はSelectManyは私にエラーを与えているんでした:

Tracks = grp.SelectMany(t => t.tracks) 

エラー:

The type arguments cannot be inferred from the usage. Try specifying the type arguments explicitly.

任意のアイデアはなぜですか?

ありがとうございます。

+0

たぶん、[この質問](http://stackoverflow.com/questions/3917249/the-type-arguments-for-method-cannot-be-inferred使い方から)はあなたにも答えます。 – meJustAndrew

答えて

1

オブジェクトtracksは、シングルトラックではなくリストです。あなたはSelectManyを使用する必要がある場合は、使用をするために、リストを選択する必要があります:あなたはリストのリストを持っているとき

Tracks = grp.Select(t => t.tracks) 

SelectManyの実際の使用量は、次のとおりです。

Projects each element of a sequence to an IEnumerable and flattens the resulting sequences into one sequence.

そうにそれを変更しますリストを単一のリストに変換する必要があります。例:

List<List<int>> listOfLists = new List<List<int>>() 
{ 
    new List<int>() { 0, 1, 2, 3, 4 }, 
    new List<int>() { 5, 6, 7, 8, 9 }, 
    new List<int>() { 10, 11, 12, 13, 14 } 
}; 

List<int> selectManyResult = listOfLists.SelectMany(l => l).ToList(); 

foreach (var r in selectManyResult) 
    Console.WriteLine(r); 

出力:

0 
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
+0

それはそれを釘付け! 'Tracks'の上にマウスを置くと、IEnumerable というメッセージが表示されます。ありがとう! – superfly71

関連する問題