tags:

views:

58

answers:

1

I need to convert the following TSQL query into a Lambda and am stuck on Count and Sum. I know I can Add .Count() and .Sum at the end of the expression - but how do I get the value as part of the expression?

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...

My expression thus far:

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
                      };
+1  A: 

I figured it out, but not sure if it is the best way to handle;

If someone finds a better way - meaning doing it in a single expression please respond!!!

I sent my calc functions to methods;

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);
        }

private Int32 Success(int ID) { …

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


        return success;
    }
KenL