加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 带有两个where子句的Linq语句

发布时间:2020-12-15 21:49:48 所属栏目:百科 来源:网络整理
导读:我正在努力解决以下问题. public class Competition{ public int Id { get; set; } public string Name { get; set; } public IListResultInfo ResultInfos { get; set; } public IListEvent ChildEvents { get; set; }}public class ResultInfo{ public int
我正在努力解决以下问题.

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”??.如果有人知道,plesae建议.

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

解决方法

你可以做:

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 ri.Season == 2013 && EventName == "Event"

正如@ von.v指出的那样,您可以通过Competition类直接访问ResultInfos,因此您可以通过以下方式简化它:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读