extracting fields from my lambda expression

I have the following line of code in a function in my unit of work:
Customer c = Customers.FindWhere(x => x.Username == username).FirstOrDefault();

Open in new window

The function that this code is in however simply wants to return the "ID" field of the customer.

Instead of asking for all the customer fields, is there a way of just saying give me the id field?

The generic function .findwhere is using e.f 4.1
Who is Participating?
käµfm³d 👽Connect With a Mentor Commented:
Yes, I had to change "Customer c" to "var c" because I wasn't sure what the type of "ID" was. You say that the function "wants to return the 'ID' field of the customer", so I expected that you have specified your function's return value to match the type of "ID"--which in this case seems to be int. The var I used should take care of any typing issues, but you can replace it with int if it makes you more comfortable.
käµfm³d 👽Commented:
If I'm understanding the issue correctly, then I think you can use:

var c = Customers.FindWhere(x => x.Username == username).Select(x => x.ID).FirstOrDefault();

Open in new window

scm0smlAuthor Commented:
I was thinking that but I get cannot implicitly convert type int to customer...?
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

scm0smlAuthor Commented:
ah right i think where i was getting confused was i though the select was just saying populate the id field of the returned customer object as opposed to just returning an int.

That works for me in this case.

If I did want to return say 3 of the 10 fields, could I do that using the lambda?
käµfm³d 👽Commented:

var c = Customers.FindWhere(x => x.Username == username)
                 .Select(x => new Customer()
                                  ID = x.ID,
                                  FName = x.FName,
                                  LName = x.LName,

Open in new window

scm0smlAuthor Commented:
ah ha...

good man.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.