Solved

extracting fields from my lambda expression

Posted on 2012-03-29
6
275 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
Technology Partners: 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Universal App 22 63
Put File 6 38
Health check of winows and webservces 5 53
C#:  Need to switch between timer loop and token driven loop 2 32
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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!
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

710 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