Solved

extracting fields from my lambda expression

Posted on 2012-03-29
6
272 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.net VB.net Errors when sending data to SQL 5 20
asp day pilot 3 19
Where did System.Data.Objects go? 2 27
Hey!! 5 33
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

770 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