English 中文(简体)
LINQ 采用总和组别的子选择?
原标题:LINQ with sub select query using sum and group by?

Can anyone help me with a LINQ query. I have converted most of it but i have a sub query in the stored procedure and i can t figure out how to do it..

这基本上是旧的储存程序(为了方便而实施的)。

 SELECT  M.Period AS  Period  ,
            C.Code AS  Group  ,
            C.ClientCode AS  Code  ,
            C.ClientName AS  Name  ,
            ( SELECT    SUM(Amount) AS Expr1
              FROM      M
              WHERE     ( ClientCode = C.ClientCode )
              GROUP BY  ClientCode
            ) AS  Amount  ,

从以上可以看出,问题就是如此。

              SELECT    SUM(Amount) AS Expr1
              FROM      M
              WHERE     ( ClientCode = C.ClientCode )
              GROUP BY  ClientCode
            ) AS  Amount 

因此,我已经与我一道工作,而且我迄今也这样做。

 var test = from c in C join h in H on c.Code 
            equals h.Code join m in M on c.ClientCode   
            equals m.ClientCode 
            select new 
            { 
                Period=m.Period,
                Group=c.Code,
                Code= c.ClientCode,
                Name= c.ClientName,
                <-- Here is where i need the sub select query above -->
            };

但是,就失去了如何做分局。 该栏的名称是你能够在旧储存程序中看到的数量。

我感谢任何反馈或帮助

THanks

最佳回答

不要肯定你问答的最后一个部分,但我是这样做的。

SELECT  M.Period AS  Period  ,
C.Code AS  Group  ,
C.ClientCode AS  Code  ,
C.ClientName AS  Name  ,
( SELECT    SUM(Amount) AS Expr1
  FROM      M
  WHERE     ( ClientCode = C.ClientCode )
  GROUP BY  ClientCode
) AS  Amount  
from C inner join M on C.ClientCode = M.ClientCode

页: 1

var test = from c in db.C
select new {
  Period = c.M.Period,
  Group = c.Code,
  Code = c.ClientCode,
  Name = c.ClientName,
  Amount = (System.Int32)
    ((from m0 in db.M
    where
      m0.ClientCode == c.ClientCode
    group m0 by new {
      m0.ClientCode
    } into g
    select new {
      Expr1 = (System.Int32)g.Sum(p => p.Amount)
    }).First().Expr1)
}
问题回答

暂无回答




相关问题
IEnumerable to array of parameter

Using linq? and XML is there a way to convert this IEnumerable to a string array of the value parameter? List<string> idList = new List<string>(); foreach (XElement idElement in word....

linq query for tag system - search for multiple tags

I have two tables, Tags(tagid, postid, tagname) and posts(postid, name, ...) now i want to make a query that returns me all posts that have a generic amount of tags. like: i want all posts that have ...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

How to filter duplicate list items

i have list of items IList with data that looks list this: GenId TestMode 1 0 1 1 3 0 3 1 4 NULL 2 NULL i want to remove the index ...

C# Grouping/Sorting a Generic List<> using LINQ

Im looking to group and sort a Generic List<>. I have a list of objects representing files and each of these objects has a FileName, FileType and FileDate property. FileType is defined as an ...

热门标签