2013-04-23 70 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你需要什么