2013-04-23 77 views
4

我正在努力解決以下問題。linq聲明與兩個where子句

public class Competition 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public IList<ResultInfo> ResultInfos { get; set; } 
    public IList<Event> ChildEvents { get; set; } 
} 


public class ResultInfo 
{ 
    public int Id { get; set;} 
    public string ResultInfoName { get; set;} 
    public int Season { get; set; } 
} 

public class Event 
{ 
    public int Id { get; set; } 
    public string EventName { get; set; } 
    public IList<ResultInfo> ResultInfos { get; set; } 
} 

我在嘗試查詢如下,試圖從比賽和事件中獲得結果信息的賽季「2013」​​。如果有人知道,請告知。

if (year.HasValue) 
{ 
    model = model.Where(x => x. ??   
} 

回答

1

你可以這樣做:

var competitions = new Competition(); // Populated competition class 

var results = (from c in competitions 
       from e in c.ChildEvents 
       from ri in e.ResultInfos 
       where ri.Season == 2013).ToList(); 

目前還不清楚爲什麼你需要一個額外的where但你可以擴展它,並有一個條款,如

where ri.Season == 2013 && EventName == "Event" 

正如@ von.v有指出,您可以直接通過競賽類訪問ResultInfos,因此您可以通過以下方式簡化:

var results = (from c in competitions 
        from ri in c.ResultInfos 
        where ri.Season == 2013).ToList(); 
+0

你會那麼需要來連接直接存儲在競爭對象中的ResultInfos的結果。 (competition.ResultInfos.Where(ri => ri.Season == 2013)); – 2013-04-23 10:24:44

+0

「競爭」中的ResultInfo如何? – 2013-04-23 10:24:55

+0

@vonv - 不錯的地方。 – 2013-04-23 10:26:21

0

我不吝嗇你想要什麼?如果你想這與結果相關信息的事件在2013年和結果相關信息的競爭也於2013年,你可以這樣做:

model.Where(c => c.ChildEvents 
        .SelectMany(ce => ce.ResultInfos) 
        .Where(ri => ri.Season == 2013).Count() > 0 
      && 
      c.ResultInfos.Where(ri => ri.Season == 2013).Count() > 0) 
    .ToList(); 

但我不知道,我undersand你需要什麼