Solved

extracting fields from my lambda expression

Posted on 2012-03-29
6
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!

761 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