开发者

How to join two datasource which return all rows and group by condition?

I have a problem when using LINQ to join two datasource. Two datasource created by a query like :

var A = (from.... group .... into grp select new { Qty = grp.Count(), Code = grp.Key.Code, Name = g开发者_开发百科rp.Key.Name });

var B = (from.... group .... into grp select new { Qty = grp.Count(), Code = grp.Key.ContCode, Name = grp.Key.ContName });

Value of 'A' will be returned like this :

    Qty-Code-Name

    1-10A-Cont10
    1-20B-Cont20
    1-30C-Cont30 

Value of 'B' will be returned like this :

    Qty-Code-Name

    1-10A-Cont10
    1-20B-Cont20
    1-30C-Cont30
    1-40D-Cont40
    1-50E-Cont50

I want to join A and B (or do somethings) and the result like this (which sum column 'Qty' if they have the same 'Code' and 'Name') :

    Qty-Code-Name

    2-10A-Cont10
    2-20B-Cont20
    2-30C-Cont30
    1-40D-Cont40
    1-50E-Cont50

How can I do it ? Please help me. Thank you very much !


Concat the two datasources and than group by code and name. Something like:

var q = from v in A.Concat(B)
        group v by new {v.Code,v.Name } into g
        select new
        {
            Qty = g.Sum(a => a.Qty),
            CodeName = g.Key.Code,
            Name = g.Key.Name
         };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜