LINQ组按对象 [英] linq group by object

查看:106
本文介绍了LINQ组按对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含另一个对象如下列表的对象:

 类CL 
{
名单,LT; A>一个 ;
公开名单< A> listofA
{
获得;组;
}
}


{
//其他属性
字符串评论;
公共字符串评论
{
获得;组;
}
}

现在我如何做一个LINQ查询,看是否评论是一些字符串这里是我的查询:

  VAR的查询=(从C在sco​​pe.Extent< CL>()
,其中c.Date> = dateFrom和放大器;&安培; c.Date< dateTo
和;&安培; c.Actions.Where(A =>(a.comment ==))
排序依据c.Date.Value.Date
组C由c.Date.Value.Date到grpDate
选择新的{grpDate.Key,项目= grpDate});



但我得到错误说:

 错误的15个操作'和;&放大器;'不能应用于类型'布尔'和'System.Collections.Generic.IEnumerable<的操作数;> 
错误13无法转换lambda表达式到类型串,因为它不是一个委托类型


解决方案

现在的问题是你使用 c.Actions.Where 。这将返回的IEnumerable< T> ,而不是布尔,但你做了检查您的where子句哪些期望一个布尔值。



您可以通过使用最有可能解决这个任何而不是其中,

  VAR的查询=(从C在sco​​pe.Extent< CL>()
,其中c.Date> = dateFrom和放大器;&安培; c.Date< dateTo
和;&安培; c.Actions.Any(A =>(a.comment ==))
排序依据c.Date.Value.Date
组C由c.Date.Value.Date到grpDate
选择新的{grpDate.Key,项目= grpDate});


I have an object which contains a list of another object as follow:

    class cl
    {
      List<a> a ;
      public List<a> listofA
      {
      get; set;
      }
    }

    class a
    {
    //other properties
       string comment ;
      public string comment
      {
      get; set;
      }
    }

Now how do i make a linq query to see if comment is of some string here is my query:

  var query = (from c in scope.Extent<cl>()
                         where  c.Date >= dateFrom && c.Date < dateTo
                         && c.Actions.Where(a => (a.comment== "") )
                         orderby c.Date.Value.Date
                         group c by c.Date.Value.Date into grpDate
                          select new { grpDate.Key, items = grpDate });

but i get error saying :

Error   15  Operator '&&' cannot be applied to operands of type 'bool' and 'System.Collections.Generic.IEnumerable<>
Error   13  Cannot convert lambda expression to type 'string' because it is not a delegate type 

解决方案

The problem is you're using c.Actions.Where. This returns an IEnumerable<T>, not a bool, but you're doing a check for your where clause which expects a boolean value.

You can most likely solve this by using Any instead of Where:

 var query = (from c in scope.Extent<cl>()
                     where  c.Date >= dateFrom && c.Date < dateTo
                     && c.Actions.Any(a => (a.comment== "") )
                     orderby c.Date.Value.Date
                     group c by c.Date.Value.Date into grpDate
                      select new { grpDate.Key, items = grpDate });

这篇关于LINQ组按对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆