2012-12-03 55 views
1

我有一個情況我需要一些過濾器適用於查詢,所以我必須代表過濾器類:多個,其中具有相關數據的條件

public class ReportNoteFilterDto 
{ 
    public int Year { get; set; } 
    public int Month { get; set; } 
} 

,我需要選擇根據此過濾器的數據。

var query = _db.User; 

if (filter.Month > 0 && filter.Month <= 12) 
    query.Select(u => new { 
     User = u, 
     Notes = u.Notes.Where(n => n.Begin.Month == filter.Month) 
    }); 

if (filter.Year > 2011) 
    query.Select(u => new 
    { 
     User = u, 
     Notes = u.Notes.Where(n => n.Begin.Year == filter.Year) 
    }); 

var results = query.Include("Notes.Foo.Bar").ToList(); //doesn't work 

我還需要選擇Notes.Foo.Bar。

我該如何做到這一點多個過濾器?

回答

1

不是你先查詢的結果分配給 query變量

你可以在這裏看到了類似的問題已經得到解決:

class Program 
{ 
    //A simple class to hold the results 
    public class Result 
    { 
     public string Item { get; set; } 

     public IQueryable<string> Collection { get; set; } 
    } 

    static void Main(string[] args) 
    { 
     var list1 = new List<Result>(); 
     var objects = list1.Select(o => new Result { Item = o.Item, Collection = o.Collection.Where(a=> a.Length == 10) }); 
     if(true) 
      objects = objects.Select(o => new Result { Item = o.Item, Collection = o.Collection.Where(a=> a.Length > 15) }); 
    } 
} 
+0

不過。選擇返回一個IQueryable但查詢是DbSet。我無法隱式轉換它和.AsQueryAble()不起作用太:( – MuriloKunze

+0

檢查更新的版本 –

+0

不起作用too.Error 不能隱式轉換類型'System.Linq.IQueryable '到'System.Linq.IQueryable ' – MuriloKunze