c# - Create DTO by joining EF entity with another object in LINQ -


i have entity framework entity

public class entiy  {     public string entityproperty1 { get; set; }     public string entityproperty2 { get; set; }     public string entityproperty3 { get; set; }     public guid? someid { get; set; } } 

and otherobject , dto contains properties that'd equal other classes.

public class otherobject {     public string otherobjectproperty1 { get; set; }     public string otherobjectproperty2 { get; set; }     public string otherobjectproperty3 { get; set; }     public guid someid { get; set; } }  public class dto {     public string entityproperty1 { get; set; }     public string entityproperty2 { get; set; }     public string entityproperty3 { get; set; }     public string otherobjectproperty1 { get; set; }     public string otherobjectproperty2 { get; set; }     public string otherobjectproperty3 { get; set; } } 

in service class iqueryable of entities , send in argument contains enumerable of otherobject. works fine when want create dtos using entities properties.

public ienumerable<dto> getdtos(ienumerable<otherobject> otherobjects)  {     return _somerepository.getall()         .where(a => otherobjects.select(b => b.someid).contains(a.someid))         .select(d => new dto()             {                 entityproperty1 = d.entityproperty1                 entityproperty2 = d.entityproperty2                  entityproperty3 = d.entityproperty3             })         .tolist(); } 

however, join in properties otherobject class. if following

public ienumerable<dto> getdtos(ienumerable<otherobject> otherobjects)  {     return _somerepository.getall()         .where(a => otherobjects.select(b => b.someid).contains(a.someid))         .select(d => new dto()             {                 entityproperty1 = d.entityproperty1                 entityproperty2 = d.entityproperty2                  entityproperty3 = d.entityproperty3                 otherobjectentity1 = otherobjects.firstordefault(a => a.someid == d.someid).otherobjectentity1             })         .tolist(); }  

i following error: "unable create constant value of type xxx. primitive types or enumeration types supported in context"

you can add property otherobject adding select first query

public ienumerable<dto> getdtos(iqueryable<otherobject> otherobjects)  {    var ids = otherobjects.select(x => x.someid);    return _somerepository.getall()           .where(x => ids.contains(x.someid)           .tolist()           .select(x =>                  new dto()                  {                     entityproperty1 = x.entityproperty1,                     entityproperty2 = x.entityproperty2,                      entityproperty3 = x.entityproperty3,                     otherobjectentity1 = otherobjects.firstordefault(p => p.someid == x.someid).otherobjectentity1                  }; } 

also extracted otherobjects' ids local variable optimize query little bit.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -