将SQL查询转换为LINQ / dot表示法

时间:2013-07-30 11:26:41

标签: linq lambda sql-to-linq-conversion

我无法将sql转换为LINQ查询。基本上我只是想把我的考试分成表格,然后计算通过的总数,失败,考试次数和合格率

SELECT testID, 
(SELECT COUNT(testID) FROM tblGrade AS b 
    WHERE b.testID= a.testID AND b.Grade < 80) AS 'Failed',
(SELECT COUNT(testID) FROM tblGrade AS b 
    WHERE b.testID= a.testIDAND b.Grade >= 80) AS 'Passed',
--taken = failed + passed, 
--passingrate = (passed / taken) * 100
FROM dbo.tblGrade AS a
GROUP BY testID
ORDER BY testID

编辑: 我的解决方案如下:它有效,但我认为它不是最好的方式,尤其是失败和通过的属性。

var xx1 = _unitOfWork.tblGrade.GetAll().GroupBy(a => new { a.testID});
var xx2 = xx1.Select(b => new
           {
             testID= b.Key.testID,
             failed = _unitOfWork.tblGrade.Query(filter: a => a.testID == b.Key.testID).Where(c => c.Grade < 80).Count(),
             passed = _unitOfWork.tblGrade.Query(filter: a => a.testID == b.Key.testID).Where(c => c.Grade >= 80).Count(),
             //taken = failed + passed, 
             //passingrate = (passed / taken) * 100
            }).ToList();

2 个答案:

答案 0 :(得分:1)

Count()可以使用谓词(Expression<Func<T, bool>>)作为参数,所以我想你可能会这样做

var xx2 = xx1.Select(b => new
           {
             testID= b.Key.testID,
             failed = b.Count(x => x.Grade < 80),
             passed = b.Count(x => x.Grade >=80),
             taken = b.Count()
            })
            .Select(b => new {
               testID = b.TestID,
               failed = b.failed,
               passed = b.passed,
               taken = b.taken,
               passingrate = b.taken == 0 ? 0.0 : ((float)b.passed / b.taken) * 100
            }).ToList();

查询语法具有(在这种情况下有用)let关键字,因此可能更容易阅读

var xx2 = from b in xx1
          let failed = b.Count(x => x.Grade < 80)
          let passed = b.Count(x => x.Grade >= 80)
          let taken = failed + passed
          select new {
              testID = b.Key.TextID,
              failed = failed,
              passed = passed,
              taken = taken,
              passingrate = taken == 0 ? 0.0 : ((float)passed / taken) * 100
          }

答案 1 :(得分:0)

我就是这样做的:

  var h = list.GroupBy(a => a.testID)
            .Select(a => {
               int _failed = a.Count(g => g.Grade < 80);
               int _passed = a.Count(g => g.Grade >= 80);
               int _rate = (int)(_passed / (double)a.Count() * 100.0);

               return new {
                 testID = a.Key,
                 failed = _failed,
                 passed = _passed,
                 taken = a.Count(),
                 rate = _rate,
              };     
            }); 

完整的测试代码:

void Main()
{
  List<aGrade> list = new List<aGrade>() {
    new aGrade() { Grade = 40, testID = 1 },
    new aGrade() { Grade = 50, testID = 1 },
    new aGrade() { Grade = 45, testID = 1 },
    new aGrade() { Grade = 70, testID = 1 },
    new aGrade() { Grade = 80, testID = 1 },
    new aGrade() { Grade = 90, testID = 1 },
    new aGrade() { Grade = 40, testID = 2 },
    new aGrade() { Grade = 50, testID = 2 },
    new aGrade() { Grade = 45, testID = 2 },
    new aGrade() { Grade = 70, testID = 2 },
    new aGrade() { Grade = 80, testID = 2 },
    new aGrade() { Grade = 90, testID = 2 },
    new aGrade() { Grade = 40, testID = 3 },
    new aGrade() { Grade = 50, testID = 3 },
    new aGrade() { Grade = 45, testID = 3 },
    new aGrade() { Grade = 70, testID = 3 },
    new aGrade() { Grade = 80, testID = 3 },
    new aGrade() { Grade = 90, testID = 3 },
  };    

  var h = list.GroupBy(a => a.testID)
            .Select(a => {
               int _failed = a.Count(g => g.Grade < 80);
               int _passed = a.Count(g => g.Grade >= 80);
               int _rate = (int)(_passed / (double)a.Count() * 100.0);

               return new {
                 testID = a.Key,
                 failed = _failed,
                 passed = _passed,
                 taken = a.Count(),
                 rate = _rate,
              };     
            }); 
  h.Dump(); 
}
// Define other methods and classes here

public class aGrade
{
    public int Grade {  get; set; }
    public int testID { get; set; }
}   

注意 - 此代码将在LinqPad中运行。 (linqpad.com)我推荐使用linqpad来测试这类代码...让你的工作变得如此简单。试试吧。