2

私は、NHibernate を使用する仕事で新しいプロジェクトを割り当てられました。SQL で簡単に記述できるクエリを、linq で実行する方法に完全に困惑しています。

したがって、クエリは次のとおりです。

select  ts.BatchID, COUNT(distinct ts.UniqID) SurveyCount
from    TeleformStaging.TeleformStaging ts
where   ts.IsRescan = 0
and     not exists (select  bfr.BatchID
                    from    TeleformStaging.BatchesForRescan bfr
                    where   bfr.BatchTrack = ts.BatchID)
group by ts.BatchID
order by ts.BatchID

「グループ化」部分は取得できると思いますが、サブクエリについてはわかりません。

アドバイスをありがとう...

4

3 に答える 3

5

多分このようなもの:

var result= (
        from ts in db.TeleformStaging
        where ts.IsRescan == false //if is a boolean else == 0
        && 
        !(
            from bfr in db.BatchesForRescan
            select bfr.BatchTrack
        ).Contains(ts.BatchID)
        orderby ts.BatchID
        group ts by ts.BatchID into g
        select new
        {
            BatchID=g.Key,
            SurveyCount=g.Select (x =>x.UniqID).Distinct().Count()
        }
    );

ここで、db は linq データ コンテキストです

編集

で行うこともできます.Any()。このような:

var result= (
        from ts in db.TeleformStaging
        where ts.IsRescan == false //if is a boolean else == 0
        && 
        !(
            from bfr in db.BatchesForRescan
            where ts.BatchID==bfr.BatchTrack
            select bfr.BatchTrack
        ).Any()
        orderby ts.BatchID
        group ts by ts.BatchID into g
        select new
        {
            BatchID=g.Key,
            SurveyCount=g.Select (x =>x.UniqID).Distinct().Count()
        }
    );

編集 1

便利なリンク:

于 2012-04-10T13:40:22.640 に答える
0
from fs in context.TeleformStaging
where !ts.IsRescan && !context.BatchesForRescan.Any(bfr=>bfr.BatchTrack == ts.BatchID)
group ts by ts.BatchID into g
    select new
    {
        BatchID=g.Key,
        SurveyCount=g.Select (x =>x.UniqID).Distinct().Count()
    }

グループ化

于 2012-04-10T13:41:54.343 に答える
0

LINQ は少し逆ですが、いくつかのラムダ式で複雑さを提供します。これはどのように見えますか:

var result = from ts in TeleformStaging.TeleformStaging
                where !ts.IsRescan && !TeleformStaging.BatchesForRescan.Any(bfr => bfr.BatchID == ts.BatchID)
                group ts by ts.BatchID into tsGrouped
                orderby tsGrouped.Key
                select new
                {
                    BatchId = tsGrouped.Key,
                    SurveyCount = tsGrouped.Select(x => x.UniqID).Distinct().Count()
                };
于 2012-04-10T13:48:32.123 に答える