tags:

views:

45

answers:

3

This is the sort of thing I want to be able to do. I know you can qury linq results again, but because these results are in groups, I don't know how to. I think the best idea I can think of is to have a query to fetch everything, then do my if statement, then do another query that groups everything (so have 3 separate queries instead of 1). Any ideas on how best to do this sort of thing?

var Result =
    from a in DB.Table

    if(Something == 0)
    {
        where a.Value > 0
    }

    group a by a.Value into b
    select new {Group = b};
+1  A: 
var result = from a in DB.Table;

if (Something == 0) 
{
 result = result.Where(r => r.Value > 0);
}

var finalResult = 
from a in result
group a by a.Value into b
select new {Group = b};
JeffN825
The first statement requires a select?
Soo
you don't need the select really, but you could have one. The point is that it should be IQueryablew<Table>. If you do result = from a in DB.Table select a or just from a in DB.Table, both have a return type of IQueryable<Table>
JeffN825
@Soo just do from a in DB.Table select a ... or Db.Table.AsQueryable(), other than that this answer is it. +1
eglasius
You shouldn't even need Db.Table.AsQueryable(). If you go to the definition of the Table property on Db, isn't it IQueryable<Table>?
JeffN825
A: 

You can restrict Linq queries with "where" statements. Try exerimenting with that. something like:

var res = from c in db.table Where c.Id == 1 Select c

Pompair
+1  A: 

Would this do what you are looking for?

        var result =
        from a in DB.Table
        group a by a.Value into g
        where !Something.Equals(0) || g.Key > 0
        select new { Group = g };
kevev22
+1 for succinctness
erash