简体   繁体   中英

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. If anyone knows, plesae advise .

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 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:

   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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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