在LINQ查询中调用自定义方法

编程入门 行业动态 更新时间:2024-10-23 17:39:35
本文介绍了在LINQ查询中调用自定义方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个这样的查询:

var q = from u in db.User select new { userId = u.UserId, userName = o.Name, userAvatar = o.AvatarCode };

然后,我必须将 AvatarCode 转换为 AvatarPath 使用自定义静态方法 Image.GetPath 。

Then, I have to transform AvatarCode to AvatarPath using custom static method Image.GetPath.

可以通过以下方式进行设置:

It's possible to make this in the following way:

var q = (from u in db.User select new { userId = u.UserId, userName = o.Name, userAvatar = o.AvatarCode }) .AsEnumerable() .Select(new { userId = u.UserId, userName = o.Name, userAvatar = Image.GetPath(o.AvatarCode) };

但是如果对象字段的数量很大,那么在第二个选择中复制所有字段是一个过分。

But if the number of object fields is large then it's an overkill to duplicate all fields in the second Select.

有没有其他选择?

例如,一些方法来标记应该执行的方法查询执行离子:

For example, some approach to mark methods that should be executed after query execution:

var q = from u in db.User select new { userId = u.UserId, userName = o.Name, userAvatar = Linq.ExecuteLater(Image.GetPath(o.AvatarCode)) };

推荐答案

没有任何这样的方法,但如果问题是只能在第二次选择中再次指定的字段数量可以这样做:

There is not any such method but if the problem is only number of fields which must be specified again in the second select you can do something like this:

var q = (from u in db.User select new { userId = u.UserId, userName = u.Name, userAvatar = u.AvatarCode }) .AsEnumerable() .Select(u => new { User = u, Path = Image.GetPath(u.AvatarCode) });

更多推荐

在LINQ查询中调用自定义方法

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

发布评论

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

>www.elefans.com

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