c# - How to rewrite SQL Union of two tables with Join in LINQ? -


i have 3 classes below:

public class {     public string id {get; set;}     public string name {get; set;}     public string age {get; set;}     public bool isemployed {get; set;}     public int roleid {get; set;} }  public class b {     public string id {get; set;}     public string name {get; set;}     public string age {get; set;}     public int roleid {get; set;} }  public class roles {     public int id {get; set;}     public string rolename {get; set;}     ... } 

suppose these classes have own tables on dbms.

i have sql query rewrite in linq (as elegantly possible)

select a.name, a.age, roles.rolename, a.isemployed   join roles on a.roleid = roles.id roles.rolename = 'admin'  union  select b.name, b.age, roles.rolename, '-' isemployed  b  join roles on b.roleid = roles.id roles.rolename = 'admin' 

currently have managed rewrite as:

var filteredclassa = c in allclassas     join role in allroles on role.id equals c.roleid     role.rolename == "admin"     select new {c.name, c.age, role.rolename, c.isemployed};  var filteredclassb = c in allclassbs     join role in allroles on role.id equals c.roleid     role.rolename == "admin"     select new {c.name, c.age, role.rolename, isemployed = "-"}; 

then can concat or union results 1 variable so:

var result = filteredclassa.union(filteredclassb); 

i don't solution, there better way above in single linq query?

thanks in advance.

another option, in 1 query:

var result = allclassas.join(allroles, c => c.id, role => role.id, (c, role) => new {c.name, c.age, role.rolename, c.isemployed})                         .where(r => r.rolename == "admin")                         .union(allclassbs.join(allroles, c => c.id, role => role.id, (c, role) => new {c.name, c.age, role.rolename, isemployed = "-"})                         .where(r => r.rolename == "admin")); 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -