简体   繁体   English

Linq声明带有两个where子句

[英]Linq statement with two where clauses

I'm trying to work out the following. 我正在努力解决以下问题。

    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; } 
}

I'm trying the query as below to try to get season "2013" of result info from competitions and events. 我正在尝试如下查询,试图从比赛和活动中获得结果信息的季节“2013”​​。 If anyone knows, plesae advise . 如果有人知道,plesae建议。

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

You can do: 你可以做:

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();

It's unclear why you need an additional where but you could extend it and have another clause such as 目前还不清楚为什么你需要一个额外的where但你可以扩展它,并有另一个条款,如

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

As @von.v has pointed out, you can access ResultInfos directly via the Competition class, so you can simplify it via: 正如@ von.v指出的那样,您可以通过Competition类直接访问ResultInfos ,因此您可以通过以下方式简化它:

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

I don't undersant what do you want ? 我不想要你想要什么? If you want the competition which have events with results infos in 2013 and result infos also in 2013 you can do this : 如果您想在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();

But I'm not sure that I undersand what you need 但我不确定我是否欠你需要的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM