© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
9 replies
becquerel

Idiomatic LINQ way to access both object and an object's property? [Answered]

var list = new List<string>();

foreach (Item item in items)
{
  foreach (string thing in item.Things)
  {
    list.Add($"{thing} + {item.OtherProperty}");
  }
}

return list;
var list = new List<string>();

foreach (Item item in items)
{
  foreach (string thing in item.Things)
  {
    list.Add($"{thing} + {item.OtherProperty}");
  }
}

return list;


I'd like to do this with LINQ. Normally I would use
.Select()
.Select()
, but if the inner operation needs both an object (
item
item
) and an inner property (
thing
thing
), I don't know of a clean way to do it. I can
.Select()
.Select()
into a tuple containing both items, but this is really ugly. Any idea?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Better way to set a property using linq?
C#CC# / help
16mo ago
Modify property of object in List [Answered]
C#CC# / help
4y ago