对Linq Lambda表达式的SQL查询 - 使用组条件加入相同的表

时间:2016-06-15 02:39:12

标签: c# lambda

任何人都可以帮助将以下SQL查询转换为c#LinQ Lambda Expression吗?感谢

tbl_CLASS

ClassID Student

Class1  A
Class1  B
Class1  C
Class2  B
Class2  C
Class3  C

结果

Class   A   B   C
Class1  Y   Y   Y
Class2  N   Y   Y
Class3  N   N   Y

SELECT  a.ClassID,
        A=case when c.ClassID is null then 'N' else 'Y' end,
        B=case when B.ClassID is null then'N' else 'Y' end,
        C='Y'
 FROM   tbl_CLASS a
        Left join tbl_CLASS b on a.ClassID=b.ClassID AND b.Student='B'
        Left join tbl_CLASS c on a.ClassID=c.ClassID AND c.Student='A'
WHERE a.Student='C'
GROUP by a.ClassID,case when c.ClassID is null then 'N' else 'Y'  end,case when B.ClassID is null then'N' else 'Y' end

3 个答案:

答案 0 :(得分:2)

List<Class1> myList = GetClass();

var query = myList
    .GroupBy(c => c.ClassID)
    .Select(g => new {
        ClassID = g.Key,
        A = g.Count(c => c.Student=="A")>0?"Y":"N",
        B = g.Count(c => c.Student=="B")>0?"Y":"N",
        C = g.Count(c => c.Student=="C")>0?"Y":"N"
    });

GetClass是获取您的数据

public class Class1
{
     public Int32 ClassID {get;set;}
     public String A{get;set;}
     public String B{get;set;}
     public String C{get;set;}
}

答案 1 :(得分:1)

当学生是动态的时,意味着您需要结果行的结果类,如下所示:

class ResultRow
{
    public ResultRow()
    {
        Students = new Dictionary<string, string>();
    }

    public string Class { get; set; }

    public IDictionary<string, string> Students { get; set; }
}

因为您需要为学生提供动态列 现在,我可以使用以下代码生成与预期结果类似的结果:

var res = tblClass
        .GroupBy(g=> g.ClassId)
        .Select(c =>
        {
            var rr = new ResultRow {Class = c.Key};
            foreach (var r in tblClass.GroupBy(gg=> gg.Student))
            {
                rr.Students[r.Key] = "N";
            }
            foreach (var r in c.Where(w=> w.ClassId == c.Key))
            {
                rr.Students[r.Student] = "Y";
            }
            return rr;
        })
        .toList();  //optional

您还可以将这两种方法添加到ResultRow类:

public string GetHeader()
{
    return Students.Aggregate("Class", (current, s) => current + "|" + s.Key);
}

public string GetSolidRow()
{
    return Students.Aggregate(Class, (current, s) => current + "|" + s.Value);
}

<强> [ Demo Here ]
HTH

答案 2 :(得分:0)

无法从System.Collections.Generic.List&lt;&gt;转换到System.Collections.Generic.IEnumerable&lt;&gt;

public SystemAccessList GetAccessList(SystemAccessList systemAccessList)
{

        var qresult = db.tbl_SystemAccessList
                .GroupBy(g => g.ClassID)
                .AsEnumerable().Select(c =>
                {
                    var rr = new ResultRow { Class = c.Key };
                    foreach (var r in db.tbl_SystemAccessList.GroupBy(gg => gg.StudentID))
                    {
                        rr.Student[r.Key] = "N";
                    }
                    foreach (var r in c.Where(w => w.ClassID == c.Key))
                    {
                        rr.Student[r.StudentID] = "Y";
                    }
                    return rr;
                }).ToList();

        systemAccessList.SystemAccessList.AddRange(qresult);
        return systemAccessList;
    }