Linq 使用 Valueinjecter 选择注入对象
我可以将 IQueryable 结果即时转换为注入对象吗?
我知道我可以在 Valueinjecter 的帮助下做到这一点:
usercategory uc1 = new usercategory(example);
usercategoryViewData ucVD1 = new usercategoryViewData();
ucVD1.injectFrom(uc1);
所以而不是这样:
from u in db.usercategories
where u.id==id
select new usercategoryViewModel {id = u.id, name = u.id, description = u.id};
我想使用类似的东西:
from u in db.usercategories
where u.id==id
select new usercategoryViewModel.InjectFrom(u);
我有 atm 的另一种选择是循环遍历 IEnumerable 并用注入的对象创建一个。
Can I convert a IQueryable result to a injected object on the fly?
I know I can do this with the help of Valueinjecter:
usercategory uc1 = new usercategory(example);
usercategoryViewData ucVD1 = new usercategoryViewData();
ucVD1.injectFrom(uc1);
So instead of this:
from u in db.usercategories
where u.id==id
select new usercategoryViewModel {id = u.id, name = u.id, description = u.id};
I would like to use something like:
from u in db.usercategories
where u.id==id
select new usercategoryViewModel.InjectFrom(u);
The other alternative I have atm is to loop through a IEnumerable and create one with injected objects instead.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这里我展示了两种方法:
here I show 2 ways of doing this:
虽然这可能是可能的,但如果
u
中有任何复杂性,那么我认为这是一个坏主意。在某些时候,您使用的 ORM(Linq-to-SQL?EF?)需要从在数据库上执行切换到在 .NET 中执行。在该边界上,它需要从数据库中找出需要哪些数据。在第一个示例中,这一点非常清楚:它只需要
u.id
。在第二个中,它不知道:它不知道InjectFrom
将从中读取哪些属性,因此它需要从 UserCategories 表中加载所有值,也许还需要加载相关对象,就在案件。Whilst that might be possible, if there's any complexity in
u
then I think it's a bad idea.At some point the ORM you're using (Linq-to-SQL? EF?) needs to switch from executing on the database to executing in .NET. At that boundary it needs to work out what data it needs from the database. In the first example, that's completely clear: it only needs
u.id
. In the second it has no idea: it doesn't know what propertiesInjectFrom
will read from it, so it will need to load all the values from the UserCategories table, and maybe related objects too, just in case.