将Lambda表达式组转换为LINQ版本

时间:2012-11-08 05:06:37

标签: c# .net linq

请考虑以下代码:

var result = data
        .GroupBy(a => groupA.Contains(a.Group) ? "A" : 
                      groupB.Contains(a.Group) ? "B" : 
                      "N/a")
        .Select(a => new
        {
            KEY = a.Key,
            VALUE = a.Count()
        });

任何人都可以帮助您将GroupBy转换为LINQ版本吗?

感谢


修改1)

我想要这种风格:

from a in ....
group a by ....
select ...

2 个答案:

答案 0 :(得分:2)

你可以尝试这样的事情,但如果有效,我会坚持你所拥有的

 var result=   from p in data
    group p by new 
   { Criterion = p.Contains(p.Group) ? "A" : p.Contains(p.Group) ? "B" : "N/a" } 
    into g  select new {KEY = g.Key, VALUE = g.Count()};

答案 1 :(得分:1)

试试这个,

        var result = from a in data
                     group a by groupA.Contains(a.Group)
                                    ? "A"
                                    : groupB.Contains(a.Group)
                                          ? "B"
                                          : "N/a"
                     into d
                     select new
                                {
                                    KEY = d.Key,
                                    VALUE = d.Count()
                                };