Linq to sql 如何在Lambda表达式中计算/求和

Linq to sql 如何在Lambda表达式中计算/求和,linq-to-sql,Linq To Sql,我需要将下面的TSQL查询转换为Lambda,并且在Count和Sum上卡住了。我知道我可以在表达式的末尾添加.Count()和.Sum,但是如何将值作为表达式的一部分 T-SQL: select b.pk, b.CreateTime, b.StartTime, b.endTime, count(a.rowid) as total, sum(case when ItemStatus = 'success' then 1 else 0 end) as Sucess from... group

我需要将下面的TSQL查询转换为Lambda,并且在Count和Sum上卡住了。我知道我可以在表达式的末尾添加.Count()和.Sum,但是如何将值作为表达式的一部分

T-SQL:

select b.pk, b.CreateTime, b.StartTime, b.endTime, count(a.rowid) as total, 
  sum(case when ItemStatus = 'success' then 1 else 0 end) as Sucess from... group by...
到目前为止,我的表达是:

var results = from context1 in dmbl1 join context2 in dmbl2 on context1.BatchLog_ID equals context2.pk
                      select new
                      {
                          CycleID = context2.pk,
                          CreateDateTime = context2.CreateDateTime,
                          StartTime = context2.startTime,
                          endTime = context2.endTime,
                          total = context1.RowID.   <<<<< This is where I need Countof
                          Success = <<< This is where I need Sumof
                      };
var results=from context1 in dmbl1 join context2 in dmbl2 on context1.BatchLog_ID=context2.pk
选择新的
{
CycleID=context2.pk,
CreateDateTime=context2.CreateDateTime,
StartTime=context2.StartTime,
endTime=context2.endTime,

total=context1.RowID.我想出来了,但不确定这是否是最好的处理方法

如果有人找到一个更好的方法-意思是用一个单一的表达,请回应

我将我的计算函数发送到方法

var results = from Context1 in dmbl1.Table
                      join Context2 in dmbl2.batchLogs on Context1.BatchLog_ID equals Context2.pk
                      select new
                      {
                          CycleID = Context2.pk,
                          CreateDateTime = Context2.CreateDateTime,
                          StartTime = Context2.startTime,
                          endTime = Context2.endTime
                      };


        foreach (var result in results)
        {
            BatchJob bj = new BatchJob();
            bj.CreationDate = result.CreateDateTime;
            bj.CycleID = result.CycleID;
            bj.EndTime = result.endTime;
            bj.StartTime = result.StartTime;
        // From Here I called Methods that returned my counts 
            bj.TotalProcessed = Total(result.CycleID);
            bj.ProcessedSuccess = Success(result.CycleID);
            bj.ProcessedFailed = Failure(result.CycleID);
            batchjobs.Add(bj);
        }
私有Int32成功(int ID) {

        var success = (from blah in context1.table
                      where context1.ID == ID 
                      && context1.ItemStatus == "success"
                      select S context1.ItemStatus
                      ).Count();


        return success;
    }

我想出来了,但不确定这是否是最好的处理方式

如果有人找到一个更好的方法-意思是用一个单一的表达,请回应

我将我的计算函数发送到方法

var results = from Context1 in dmbl1.Table
                      join Context2 in dmbl2.batchLogs on Context1.BatchLog_ID equals Context2.pk
                      select new
                      {
                          CycleID = Context2.pk,
                          CreateDateTime = Context2.CreateDateTime,
                          StartTime = Context2.startTime,
                          endTime = Context2.endTime
                      };


        foreach (var result in results)
        {
            BatchJob bj = new BatchJob();
            bj.CreationDate = result.CreateDateTime;
            bj.CycleID = result.CycleID;
            bj.EndTime = result.endTime;
            bj.StartTime = result.StartTime;
        // From Here I called Methods that returned my counts 
            bj.TotalProcessed = Total(result.CycleID);
            bj.ProcessedSuccess = Success(result.CycleID);
            bj.ProcessedFailed = Failure(result.CycleID);
            batchjobs.Add(bj);
        }
私有Int32成功(int ID) {

        var success = (from blah in context1.table
                      where context1.ID == ID 
                      && context1.ItemStatus == "success"
                      select S context1.ItemStatus
                      ).Count();


        return success;
    }

就像在SQL中一样,您需要GROUPBY语句就像在SQL中一样,您需要GROUPBY语句