Linq to sql - 用groupby嵌套选择

时间:2017-06-09 15:57:06

标签: linq

我有以下SQL查询:

 SELECT * FROM [dbo].[Bil_Audit_BillingParameter] WHERE ([Status4Eyes]!='APPROVED' OR [Status4Eyes] IS NULL )AND
        [LastComment] != '' AND
        (Service='EUR' OR Service='S1') AND
        [AudId] IN (SELECT MAX([AudId])
        FROM [Bil_Audit_BillingParameter]
        GROUP BY ID) UNION SELECT * FROM 
                                 [dbo].Bil_Audit_BillingParameter] WHERE ([Status4Eyes]!='APPROVED' OR [Status4Eyes] IS NULL) AND
                                 [LastComment] != '' AND
                                 (Service='EUR' OR Service='S1') AND
                                 [AudId] IN (SELECT MIN([AudId])
                                 FROM [Bil_Audit_BillingParameter]
                                 GROUP BY ID)

如何转换为linq?我尝试了以下内容,但最终出现了错误。

public IQueryable<ParameterUpdates> Get(ODataQueryOptions op)
    {


        var audIdMax = db.ParameterUpdates.GroupBy(x => x.ID).Select(v => v.Max(x => x.AudId));
        var audIdMin = db.ParameterUpdates.GroupBy(x => x.ID).Select(a => a.Min(x => x.AudId));

        var results1 = db.ParameterUpdates.Where(p => (!p.Status4Eyes.Contains("Approved") || string.IsNullOrEmpty(p.Status4Eyes)) && 
                                                !p.LastComment.Contains("NULL") &&
                                                (p.Service.Contains("EUR") || p.Service.Contains("S1")) &&
                                                p.AudId.Equals(audIdMax));

        var results2 = db.ParameterUpdates.Where(p => (!p.Status4Eyes.Contains("Approved") || string.IsNullOrEmpty(p.Status4Eyes)) &&
                                                !p.LastComment.Contains("NULL") &&
                                                (p.Service.Contains("EUR") || p.Service.Contains("S1")) &&
                                                p.AudId.Equals(audIdMin));
        var results = results1.Union(results2);

        return results.AsQueryable();
    }

有人可以帮忙。

2 个答案:

答案 0 :(得分:0)

你可以试试这个,第一组按id创建过滤器输出。然后使用实际表将内部加入最大或最小审计ID的过滤器并找到结果。

var filter = ParameterUpdate.Where(p => (p.status == "Approved" || p.status == null)
                                         && p.lastComment != ""
                                         &&(p.service=="EUR" || p.service=="S1")).GroupBy(p => p.id);

var result = (from p in ParameterUpdate
              from f in filter
              where p.auditId == f.Min(m => m.auditId) ||
                    p.auditId == f.Max(m => m.auditId)
              select p);

答案 1 :(得分:0)

public IQueryable<ParameterUpdates> Get()
    {
        var groupMax = db.ParameterUpdates
        .GroupBy(u => u.ID)
        .Select(grp => grp.Max(x => x.AudId));

        var groupMin = db.ParameterUpdates
       .GroupBy(u => u.ID)
       .Select(grp => grp.Min(x => x.AudId));

        var results = (db.ParameterUpdates.
            Where(r => (r.Status4Eyes != "APPROVED" || r.Status4Eyes == null) &&
            (r.LastComment != null) &&
            (r.Service == "EUR" || r.Service == "S1") &&
            (groupMax.Contains(r.AudId) || groupMin.Contains(r.AudId))
            )).ToList();


        return results.AsQueryable();