vcRobe
vcRobe

Reputation: 1781

LINQ calling a method inside Select passing as parameter the selected item

I've a LINQ statement where inside the Select I need to call a method that has as parameter the item selected in the query.

Here's an example:

List<Foo> foos = new List<Foo> {....};

float GetPrice(Foo) {....}

var query = foos
  .Where(x => x.ID == 1)
  .Select(x => new
  {
    aaa = GetPrice(????)
  });

How can I specify the selected Foo in the call to GetPrice(????) ?

Upvotes: 0

Views: 9801

Answers (1)

Rion Williams
Rion Williams

Reputation: 76597

The x within your Select() statement is going to represent a Foo object, since you are querying from a List<Foo>, so you should just be able to pass in x to your GetPrice() method :

aaa = GetPrice(x)

So your entire code would look something like this :

var query = foos.Where(x => x.ID == 1)
                .Select(x => new {
                      aaa = GetPrice(x)
                });

Upvotes: 3

Related Questions