c# - Conditional linq query -


what best approach converting sql linq?

i've earlier made views in database based on such sql, , query view linq. i've know if there other approaches.

the sql finds assigned object task. task table contains 3 foreign key columns, assigned may department, position or person. 1 allowed.

sql:

select   id,          title,          assigned_to = (case             when idassigneddepartment not null (select description department id = idassigneddepartment)             when idassignedposition not null (select description position id = idassignedposition )             when idassignedperson not null (select fullname person id = idassignedperson )                            end)     task 

using linqtoef

you can write this:

var q = t in task         dep in department.where(x => x.id == t.idassigneddepartment).defaultifempty()         pos in position.where(x => x.id == t.idassignedposition).defaultifempty()         per in person .where(x => x.id == t.idassignedperson).defaultifempty()         select new         {             t.id,             t.title,             assigned_to = t.idassigneddepartment != null ? dep.description :                           t.idassignedposition != null ? pos.description :                           t.idassignedperson != null ? per.fullname :                            null         }; 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -