CodyB1150
CodyB1150

Reputation: 17

Multiple Select Linq Query

Is there a way to select multiple properties using the .Select() query?

Something like this

model.Owners = eventResponse.Records.Select(x => x.Owner.Name && x.Subject).ToList();

Now I know that syntax doesn't work and it's pseudo-code, but is there a way to do something that produces the same result?

Upvotes: 0

Views: 78

Answers (4)

Pandurang Choudekar
Pandurang Choudekar

Reputation: 382

Yes. If it is connected via foreign key, then you should need to use .include extension. It's working for sure, especially for pseudo-code.

model.Owners = eventResponse.Records.include("Owner").Select(x => x.Owner.Name && x.Subject).ToList();

Upvotes: 0

Maks Shapovalov
Maks Shapovalov

Reputation: 184

You should use anonymous type:

model.Owners = eventResponse.Records.Select(x => new {Name = x.Owner.Name, Subject = x.Subject)).ToList();

Upvotes: 4

itsme86
itsme86

Reputation: 19486

You can create an anonymous type or a tuple:

eventResponse.Records.Select(x => new { Name = x.Owner.Name, Subject = x.Subject }).ToList();

Or...

eventResponse.Records.Select(x => (Name: x.Owner.Name, Subject: x.Subject)).ToList();

Upvotes: 2

Patrick Beynio
Patrick Beynio

Reputation: 858

You could use a tuple like:

model.Owners = eventResponse.Records.Select(x => (x.Owner.Name, x.Subject)).ToList();

Upvotes: 1

Related Questions