0

通常の SQL クエリ コマンドで次のような linq を作成する必要があります。

select t1.vendorcode, t1.location, sum(t1.sales)
from table1 t1
where t1(vendorCode, location) in
      (select t2.vendorCode, t2.location from table2 t2) 
groupby t1.vendorCode, t1.location

次のようにlinqを構築します。

query = from t1 in table1
where ...
join t2 in table2 on new
{
  t2.vendorcode, t2.location
} equals new
{ 
  t1.vendorcode, t1.location 
}

私が持っている質問は次のとおりです。このlinqをどのように構築すればよいですか? 別のサブクエリが必要ですか、それともさらに追加group byしてステートメントを選択して、この linq を完成させることはできますか?

4

2 に答える 2

1

別の句を追加する必要はありませんgroup by。合計を選択するだけです。

var query = from t1 in table1
            join t2 in table2 
              on new { t1.vendorcode, t1.location } equals
                 new { t2.vendorcode, t2.location }
            group t1 by new { t1.vendorcode, t1.location } into g
            select new { 
                g.Key.vendorcode,
                g.Key.location, 
                g.Sum(t1 => t1.sale)
            };

これは、特定のベンダーコード/場所のペアでレコードが1つしかない場合に機能します。ただし、そのようなレコードが複数table2存在する可能性がある場合は、機能しません。おそらく、次のようなものが必要になります。

var query = from t1 in table1
            where table2.Select(t2 => new { t2.vendorcode, t2.location })
                        .Contains(new { t1.vendorcode, t1.location })
            group t1 by new { t1.vendorcode, t1.location } into g
            select new { 
                g.Key.vendorcode,
                g.Key.location, 
                g.Sum(t1 => t1.sale)
            };

それは論理的にあなたの「存在する」バージョンです。

于 2013-01-29T06:48:50.847 に答える
0

これでできるはず

var query =
    from t1 in table1

    join t2 in table2
    on new { vc = t1.vendorcode, lc = t1.location }
    equals new { vc = t2.vendorcode, lc = t2.location }

    group t1 by new { vc = t1.vendorcode, lc = t1.location };
于 2013-01-29T06:38:58.123 に答える