0

ここで、Linq2Sqlを使用してテーブルを結合し、リンクされたレコードの量をカウントする方法を見つけました。LINQ-左結合、Group By、およびカウント

私はそれを実装しました、そしてそれは私にとってうまくいきます:次の式

var v = from c in db.GetTable<Country>()
        join t0 in db.GetTable<Team>() on c.Id equals t0.CountryId into t1
        from team in t1.DefaultIfEmpty()
        group team by c.Id into teamsGrouped
        select new CountryTeamsInfo
            {
                CountryId = teamsGrouped.Key,
                TeamsTotal = teamsGrouped.Count(),
                // TeamsWithoutOwnerFree = teamsGrouped.Count(t => t.OwnerId==0)
            }
            ;
         List<CountryTeamsInfo> res = v.ToList();

次のクエリを生成します。

SELECT c.Id, Count(*) as c1
FROM countries c
LEFT JOIN teams t1 ON c.Id = t1.Country
GROUP BY c.Id 

実際、OwnerIdフィールドが0に等しくないリンカーレコードもカウントする必要があります。

linq式(TeamsWithoutOwnerFree = teamGrouped.Count(t => t.OwnerId == 0))のその行のコメントを解除する必要があるようですが、それは機能しません。実行しようとすると、エラーが発生します。

指定されたキーが辞書に存在しませんでした

クエリがSQLログファイルに届かず、デバッガーで調べることができません。

追加の基準を満たす「チーム」テーブルからこれらの行をカウントする適切な方法は何でしょうか。

PS重要な場合は、C#4.0、MySql 5.1、BLToolkit4.1を使用します

4

3 に答える 3

1

GroupJoin()クエリに使用してみてください:

var result = db.GetTable<Country>()
               .GroupJoin(db.GetTable<Team>(),
                   c => c.Id,
                   t => t.CountryId,
                   (country, teams) => new
                   {
                       CountryId = country.Id,
                       TeamsTotal = teams.Count(),
                       TeamsWithoutOwnerFree = teams.Count(t => t.OwnerId == 0)
                   })
               .ToList();
于 2012-04-18T11:01:37.333 に答える
0

私の環境で動作するクエリは次のとおりです。

from c in db.GetTable<Country>()
where c.Allowed
select new CountryTeamsInfo
{
    CountryId = c.Id,
    TeamsTotal = db.GetTable<Team>().Count(t => t.CountryId == c.Id && t.Allowed),
    TeamsHasOwner = db.GetTable<Team>().Count(t => t.CountryId == c.Id && t.Allowed && t.OwnerId != 0),
}

最善の解決策ではありません (各サブクエリでチームの選択基準 t.Allowed を繰り返す必要があります) が、それでもうまく機能し、生成された SQL は良好です。

于 2012-04-21T18:06:01.823 に答える
0

RePierre のヘルプのおかげで、適切なクエリを見つけることができました。

var v = db.GetTable<Country>().Where(country => country.Allowed)
                    .GroupJoin(
                        db.GetTable<Team>(),
                        country => country.Id,
                        team => team.CountryId,
                        (country, teams) => new CountryTeamsInfo
                                                {
                                                    CountryId = country.Id,
                                                    TeamsTotal = teams.Count(),
                                                    TeamsWithoutOwnerFree = teams.Count(t => t.OwnerId != 0),
                                                }
                    ).GroupJoin(
                        db.GetTable<Team>().Where(te=>te.OwnerId==0),
                        cti => cti.CountryId,
                        team => team.CountryId,
                        (cti, teams) => new CountryTeamsInfo
                        {
                            CountryId = cti.CountryId,
                            TeamsTotal = cti.TeamsTotal,
                            TeamsWithoutOwnerFree = teams.Count(t => t.OwnerId != 0),
                        }
                    )
                    ;

私の懸念は、2 つのサブクエリを使用していることです...それを最適化しようとします。どんなアイデアでも素晴らしいでしょう

PS 実際には、生成された SQL クエリも醜く見えます。

SELECT
cti.Id as Id1,
cti.c1 as c11,
(
    SELECT
        Count(*)
    FROM
        teams te
    WHERE
        cti.Id = te.Country AND te.User = 0
) as c2
FROM
(
    SELECT
        country.Id,
        (
            SELECT
                Count(*)
            FROM
                teams t1
            WHERE
                country.Id = t1.Country
        ) as c1
    FROM
        countries country
    WHERE
        country.allow
于 2012-04-19T13:24:56.410 に答える