从ObjectConext获取值 [英] Getting values from ObjectConext

查看:123
本文介绍了从ObjectConext获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EntityFramework从数据库中获取数据。



考虑我有一张桌子



买家
{
guid id,
datetime date,
guid userid reference user(id)
}

user
{
guid id,
string name
}



将上述类视为DB中的表。

我想在买家列表中返回用户名。通常的方法是buyer.User.name。

但为此我们需要在objectcontext中使用Include。但我想要使用Include(因为它使查询添加外部联接。在我的情况下,这个外部类增加了我更多的成本)



我实际上尝试在Buyer类中添加用户名作为参数并访问该值。

我该怎么做..



更像



< pre lang =c#> this .objectcontext.Buyer。 select (x => new Buyer( ){x.username = x.Users.name})。ToList();





但这里我的其他参数(如日期, id)必须自己分配。

解决方案

我在使用包含减慢我的linq性能方面遇到了同样的问题。



为了解决这个问题我在linq上使用select new来手动填充我需要的属性(在你的情况下是User.Name)到一个具有相同名称(Buyer)和命名空间的部分类中物质中的阶级。因此,可以访问属性(用户名),就好像它是表的一部分。



this.objectcontext.Buyer.select(x => new Buyer(){ x.username = x.Users.name})。ToList();



这正是我之前解释过的,你只需要手动设置一个连接因为linq不会猜到你的名字



lmk


I m using EntityFramework to get the data from the DB.

consider I have a table

Buyer
{
   guid id,
   datetime date,
   guid userid references user(id)
}

user
{
  guid id,
  string name
}


consider the above class as table in DB.
I want to return user name in buyer list. The usual way is buyer.User.name.
but for this we need to use Include in objectcontext. But I want with out using the Include (because it makes the query to add a outer join. In my case this outer class adds me more cost)

I am actually trying to add username as a parameter in Buyer class and access the value.
How can i do this..

more like

this.objectcontext.Buyer.select(x=>new  Buyer(){x.username=x.Users.name}).ToList();



But here my other parameters (like date,id) has to be assigned itself.

解决方案

I had the same problems concerning the use of includes slowing down my linq performance.

To solve that problem I had use "select new" on linq to manually populate the properties I needed (User.Name, on your case) into a partial class having the same name (Buyer) and namespace of the class in matter. Therefore the property (Username) is accessible as if it was part of the table.

this.objectcontext.Buyer.select(x=>new Buyer(){x.username=x.Users.name}).ToList();

that is exactly what i've explained earlier, you just need to manually set a join, cause linq won't guess the name for you

lmk


这篇关于从ObjectConext获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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