Linq to Entities选择带lambda的子句 [英] Linq to Entities Select clause with lambda

查看:114
本文介绍了Linq to Entities选择带lambda的子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个新项目,我们正在使用Entity Framework,并且开发人员可能希望尽可能使用lambda查询。有一件事我们很难弄清楚如何专门选择两列。还如何选择不同的。我们有一个表,供应商有多个条目,但我们只想得到供应商列表并加载到字典对象。它失败了,因为它正在尝试添加已添加的键值。进行以下查询。

 字典< int,string> dict = new Dictionary< int,string>(); 
dict = GetWamVendorInfo()。AsEnumerable()
.Where(x => x.vendor_name!= null&& x.vendor_id!= null)
//。 x => x.vendor_id).Distinct()
.Take(2)
.ToDictionary(o => int.Parse(o.vendor_id.ToString()),o => o 。供应商名称);

我想做的只是选择vendor_id和vendor_name,这样我们可以得到不同的记录。



任何帮助将不胜感激。



谢谢,



Rhonda

解决方案

使用匿名类型:

  //更早一点查询
.Select(x => new {VendorId = x.vendor_id,VendorName = x.vendor_name})
.Distinct()
.ToDictionary(o => o.VendorId,o => o.VendorName);

我删除了对 Take(2)因为你不明白为什么你会想要它 - 也删除了解释 VendorId ,我本来希望已经是一个整数类型。 / p>

请注意,您应该几乎肯定从您的查询中删除 AsEnumerable 呼叫 - 目前您将所有供应商都采集到所有并使用LINQ to Objects进行过滤。还没有点创建一个空字典,然后完全忽略它。我怀疑你的完整查询应该是:

  var vendors = GetWamVendorInfo()
.Select(x => new {VendorId = x.vendor_id,
VendorName = x.vendor_name})
.Distinct()
.ToDictionary(o => o.VendorId,
o => o。供应商名称);

除此之外,你应该问你的开发人员为什么他想要使用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

解决方案

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.

这篇关于Linq to Entities选择带lambda的子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆