Solved

extracting fields from my lambda expression

Posted on 2012-03-29
6
273 Views
Last Modified: 2012-03-29
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
0
Comment
Question by:scm0sml
  • 3
  • 3
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37784432
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

0
 

Author Comment

by:scm0sml
ID: 37784446
I was thinking that but I get cannot implicitly convert type int to customer...?
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37784498
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.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:scm0sml
ID: 37784508
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?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37784524
Sure:

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

Open in new window

0
 

Author Comment

by:scm0sml
ID: 37784629
ah ha...

good man.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question