Can I select multiple objects in a Linq query

后端 未结 7 453
有刺的猬
有刺的猬 2021-02-02 06:48

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 t

7条回答
  •  逝去的感伤
    2021-02-02 07:41

    Edit: Sorry, misunderstood your original question, so rewrote answer.

    You could use the "SelectMany" operator to do what you want:

    IEnumerable drew =
               (from fixture in fixtures
                where fixture.Played && (fixture.HomeScore == fixture.AwayScore)
                      select new List()
                                 { HomeTeam = fixture.HomeTeam,
                                   AwayTeam = fixture.AwayTeam
                                 }).SelectMany(team => team);
    

    This will return a flattened list of teams that drew.

提交回复
热议问题