使用new进行linq查询的更好方法(better way of doing a linq query using new)

编程入门 行业动态 更新时间:2024-10-27 06:32:20
使用new进行linq查询的更好方法(better way of doing a linq query using new)

有没有更好的方法 - 使用Entity Framework从标准SQL服务器中提取:

我正在使用telerik radgrid并提前将字段名称放入列中,因此它将与从linq查询中提取的数据匹配,如:

var zips = (from z in db.zipcode_lookup select z).ToList(); zipCodeGrid.DataSource = zips;

它工作正常。 但我需要将两个字段合并为一个,所以我现在需要这样做:

var zips = (from z in db.zipcode_lookup select new { zip_code = z.zip_code, fullName = z.driver_details.first_name + " " + z.driver_details.last_name, driver_id = z.driver_id, zone = z.zone } ).ToList();

这也很好。 我想要做的是不必重新创建已经存在的所有字段(zip_code,driver_id和zone)。

我为什么不能这样做:

var zips = (from z in db.zipcode_lookup select new { z, fullName = z.driver_details.first_name + " " + z.driver_details.last_name } ).ToList();

就像我说的那样有效,但我总是遇到这种情况,如果有更好的方法,我真的很感激知道它。

编辑 -

这有点短(就像我说的,在这种情况下没什么大不了的,但有时会有20或30列更大的交易)

var zips = (from z in db.zipcode_lookup select new { z.zip_code, z.zone, z.driver_id, fullName = z.driver_details.first_name + " " + z.driver_details.last_name } ).ToList();

我编辑了某人的答案,没有意识到必须经过同行评审。 出于某种原因,这适用于下面介绍的新类解决方案:

var zips = (from z in db.zipcode_lookup.ToList() select new ZipData(z)).ToList();

Is there a better way of doing this - using Entity Framework pulling from a standard SQL server:

I am using a telerik radgrid and putting the field names into the columns ahead of time so it will match up with the data pulled from a linq query like:

var zips = (from z in db.zipcode_lookup select z).ToList(); zipCodeGrid.DataSource = zips;

and it works fine. But I need to combine two fields into one so I now need to do this:

var zips = (from z in db.zipcode_lookup select new { zip_code = z.zip_code, fullName = z.driver_details.first_name + " " + z.driver_details.last_name, driver_id = z.driver_id, zone = z.zone } ).ToList();

Which also works fine. What I am trying to do is to not have to recreate all the fields that are already there (the zip_code, driver_id and zone).

Why can I not do something like:

var zips = (from z in db.zipcode_lookup select new { z, fullName = z.driver_details.first_name + " " + z.driver_details.last_name } ).ToList();

Like I said it works, but I come across this all the time and if there is a better way of doing it I would really appreciate knowing it.

EDIT -

This is a bit shorter (like I said, in this case no big deal, but sometimes with 20 or 30 columns much bigger deal)

var zips = (from z in db.zipcode_lookup select new { z.zip_code, z.zone, z.driver_id, fullName = z.driver_details.first_name + " " + z.driver_details.last_name } ).ToList();

I edited someone's answer not realizing it had to be peer reviewed. for some reason this works with the new class solution presented below:

var zips = (from z in db.zipcode_lookup.ToList() select new ZipData(z)).ToList();

Joe

最满意答案

更新:这是一个可怕的答案,因为它不适用于EF。 我已经在问题中添加了EF标签以便澄清。 请忽略这个答案,并在你的谎言中投票,因为我自己不能这样做。 [MH]

一种可能的解决方案是声明一个包含您要返回的数据的小类:

public class ZipData { public ZipData (zip_code_lookup z) { this.zip_code = z.zip_code; this.full_name = string.Format("{0} {1}", z.driver_details.first_name, z.driver_details.last_name); this.driver_id = z.driver_id; this.zone = z.zone; } public string zip_code { get; set; } public string full_name { get; set; } public int driver_id { get; set; } public string zone { get; set; } }

然后,按如下方式更改Linq表达式:

var zips = (from z in db.zipcode_lookup select new ZipData(z)) .ToList();

这样,如果你需要全名,你就拥有它。 如果你不这样做,你可以忽略它。 无论哪种方式,构造函数都会为您构建它。 而且您不再使用匿名类型(它不受匿名类型的限制)。

UPDATE: This is a horrible answer, as it doesn't work with EF. I've added the EF tag to the question to clarify. Please ignore this answer and downvote it at your liesure, as I can't do so myself. [MH]

A possible solution is to declare a small class that holds the data you're returning:

public class ZipData { public ZipData (zip_code_lookup z) { this.zip_code = z.zip_code; this.full_name = string.Format("{0} {1}", z.driver_details.first_name, z.driver_details.last_name); this.driver_id = z.driver_id; this.zone = z.zone; } public string zip_code { get; set; } public string full_name { get; set; } public int driver_id { get; set; } public string zone { get; set; } }

Then, change your Linq expression as follows:

var zips = (from z in db.zipcode_lookup select new ZipData(z)) .ToList();

This way, if you need the full name, you have it. If you don't, you can ignore it. Either way, the constructor builds it for you. And you are no longer working with an anonymous type (it doesn't suffer from the constraints of anonymous types).

更多推荐

本文发布于:2023-08-07 22:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466140.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   linq   query

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!