Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to Entities Select clause with lambda

I am working on a new project and we are using Entity Framework and the dev lead would like to use lambda queries whenever possible. One thing we are having a hard time figuring out is how to select two columns specifically. Also how to select distinct. We have a table that has multiple entries for a vendor but we want to just get a list of vendors and load to a dictionary object. It fails because as written it is trying to add a key value that has already been added. Take the following query.

Dictionary<int, string> dict = new Dictionary<int, string>();
        dict = GetWamVendorInfo().AsEnumerable()
                    .Where(x => x.vendor_name != null && x.vendor_id != null)
                    //.Select(x => x.vendor_id).Distinct()
                    .Take(2)
                    .ToDictionary(o => int.Parse(o.vendor_id.ToString()), o => o.vendor_name);

What I would like to do is select just vendor_id and vendor_name so we can get just the distinct records.

Any help would be greatly appreciated.

Thanks,

Rhonda

like image 791
Rhonda Avatar asked Sep 02 '11 16:09

Rhonda


People also ask

Can you use lambda expression instead of LINQ query?

So performance-wise, there's no difference whatsoever between the two. Which one you should use is mostly personal preference, many people prefer lambda expressions because they're shorter and more concise, but personally I prefer the query syntax having worked extensively with SQL.

Is LINQ faster than lambda?

In some cases LINQ is just as fast if not faster than other methods, but in other cases it can be slower. We work on a project that we converted to linq and the data lookup is faster but the merging of data between two tables is much slower.

What is lambda expressions in LINQ?

The term 'Lambda expression' has derived its name from 'lambda' calculus which in turn is a mathematical notation applied for defining functions. Lambda expressions as a LINQ equation's executable part translate logic in a way at run time so it can pass on to the data source conveniently.

What is LINQ to Entities?

LINQ to Entities provides Language-Integrated Query (LINQ) support that enables developers to write queries against the Entity Framework conceptual model using Visual Basic or Visual C#. Queries against the Entity Framework are represented by command tree queries, which execute against the object context.


2 Answers

Use an anonymous type:

// earlier bit of query
.Select(x => new { VendorId = x.vendor_id, VendorName = x.vendor_name } )
.Distinct()
.ToDictionary(o => o.VendorId, o => o.VendorName);

I've removed the call to Take(2) as it wasn't clear why you'd want it - and also removed the parsing of VendorId, which I would have expected to already be an integer type.

Note that you should almost certainly remove the AsEnumerable call from your query - currently you'll be fetching all the vendors and filtering with LINQ to Objects. There's also no point creating an empty dictionary and then ignoring it entirely. I suspect your complete query should be:

var vendors = GetWamVendorInfo()
                      .Select(x => new { VendorId = x.vendor_id,
                                         VendorName = x.vendor_name } )
                      .Distinct()
                      .ToDictionary(o => o.VendorId,
                                    o => o.VendorName);

As an aside, you should ask your dev lead why he wants to use lambda expressions (presumably as opposed to query expressions) everywhere. Different situations end up with more readable code using different syntax options - it's worth being flexible on this front.

like image 169
Jon Skeet Avatar answered Oct 21 '22 08:10

Jon Skeet


Just use an anonymous object:

var vendors = GetWamVendorInfo().AsEnumerable()
                .Where(x => x.vendor_name != null && x.vendor_id != null)
                .Select(new {x.vendor_id, x.vendor_name})
                .Take(2)

That's it. You can now work with vendors[0].vendor_id, vendors[0].vendor_name, and so on.

like image 35
Benjamin Pollack Avatar answered Oct 21 '22 08:10

Benjamin Pollack