问题
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
回答1:
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 });
回答2:
You are trying to use the result of c.Actions.Where(a => (a.comment== "") )
, which is IEnumerable
, as a bool
. If I understand correctly, you may want to use Any
instead of Where
in this expression - or other aggregation function like All
.
回答3:
c.Actions.Where(a => (a.comment== "") )
returns a list of actions, but you're using it as a boolean (with &&
). You should either use .Any()
to test whether there is something that matches, or refactor so that the true/false tests are separate from the Where()
I'd give more specific advice, but I'm not sure why you're comparing to an empty string.
来源:https://stackoverflow.com/questions/13403483/linq-group-by-object