c# - Get foreign key value without join In Entity-to-Linq -


employee table has departmentid foreign key relation department table's id column.

now if select rows employee table, should department name respective departmentid in foreign key column of employee table.

i'm using entity framework can thing using join via creating object of department table.

output should like:

employeename    employeeaddress  employeedepartment     abc            xyz                  

it should automatically fetch foreign key value employee

lamientities1 lam = new lamientities1();         var query =( sls in lam.sales.asenumerable() join in lam.items on sls.itemid equals it.id orderby sls.id descending                     select new                      {                         itemname = it.itemname,                         totalamounts = sls.amount,                         remarks=sls.remarks,                         dates = sls.date                      }).take(20);          gridview1.datasource = query;         gridview1.databind(); 

here how can remove join , use directly itemname using itemid forign key

i guess looking for:

you can make dto(data transfer object), transport data between layers

public class employeewithdepartmentdto {     public string employeename { get; set; }     public string employeeaddress { get; set; }     public string departmentname { get; set; } } 

and results this:

var result = employeerepository.select(e => new employeewithdepartmentdto { employeename = e.name, employeeaddress = e.address, departmentname = e.department.name });  foreach (var employeewithdepartmentdto in result)      console.writeline($"name: {employeewithdepartmentdto.employeename}, address: {employeewithdepartmentdto.employeeaddress}, department: {employeewithdepartmentdto.departmentname}"); 

here example in dotnetfiddle

if dont wanna create dto class, can make query using anonymous objects

var result = employeerepository.select(e => new { employeename = e.name, employeeaddress = e.address, departmentname = e.department.name }); 

or can query this, using linq-to-sql:

var result = e in employeerepository                 select new employeewithdepartmentdto                 {                     employeename = e.name,                     employeeaddress = e.address,                     departmentname = e.department.name                 } 

if using ef eager loading, must use include, way:

lamientities1 lam = new lamientities1();     var query =( sls in lam.sales.include("item")                 orderby sls.id descending                 select new                  {                     itemname = sls.item.itemname,                     totalamounts = sls.amount,                     remarks=sls.remarks,                     dates = sls.date                  }).take(20); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -