我可以在LINQ查询选择多个对象 [英] Can I select multiple objects in a Linq query

查看:373
本文介绍了我可以在LINQ查询选择多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新到LINQ所以裸陪我。我可以返回多个项目的选择?比如我有照明灯(觉得足球(或足球的美国佬)装置)的列表。每个夹具包括一个主客场的团队和一个主客场比分。我希望得到所有提请团队。我想使用类似

I'm very new to Linq so bare with me. Can I return more than one item in a select? For instance I have a List of Fixtures (think football (or soccer for the yanks) fixtures). Each fixture contains a home and away team and a home and away score. I want to get all the teams that drew. I want to use something like

IEnumerable<Team> drew = from fixture in fixtures
                         where fixture.Played && (fixture.HomeScore == fixture.AwayScore)
                         select fixture.HomeTeam && fixture.AwayTeam;



我知道这语法不正确,我不知道是否有可能做到这一点。我需要两个查询,然后将它们连接起来。

I know this syntax is incorrect, what I don't know is if it's possible to do this. Would I need two queries and then concatenate them?

编辑:这的确是一个学习的东西,所以它不是至关重要的,任何特定的方式来实现这一目标。基本上,在这个阶段我要的是,已制定球队的名单。用法的例子可能是对灯具的一个给定的名单我可以找到所有的绘制球队,这样我可以得到1分(3一胜,0损失)更新自己的表中的排名。

干杯
詹姆斯

推荐答案

我觉得你寻找联盟的方法如下:

I think you're looking for the Union method as follows:

IEnumerable<Team> drew = (from fixture in fixtures
                     where fixture.Played 
                        && (fixture.HomeScore == fixture.AwayScore)
                     select fixture.HomeTeam)
                     .Union(from fixture in fixtures
                     where fixture.Played 
                        && (fixture.HomeScore == fixture.AwayScore)
                     select fixture.AwayTeam);

这篇关于我可以在LINQ查询选择多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆