Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I m using EntityFramework to get the data from the DB.

consider I have a table

C#
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

C#
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.
Posted
Updated 9-Jun-14 22:54pm
v2

1 solution

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
 
Share this answer
 
Comments
Prasaad SJ 14-Jun-14 23:20pm    
Hi goathik,
Thanks for your reply. Actually this solution works fine if I have a class/table that have a limited number of column. But in one my case I have a table of 140 columns. I m damn sure I cannot manually set each column, because if I add a new column to the same table , I have to change all the methods in which I implemented this. You got any solution for this.. ??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900