Linq2entities, jointures multiples

Linq2entities, jointures multiples - C#/.NET managed - Programmation

Marsh Posté le 20-05-2011 à 12:01:03    

Hello,
 
Situation un poil simplifiée.
J'ai 3 tables :
AnimalCategory
AnimalType
Animal
 
AnimalCategory a 1-n AnimalType
AnimalType a 0-n Animal
 
Ma requête doit me retourner
AnimalCategory ->AnimalTypes -> Animal (s'ils existent, avec une condition sur une autre table).
 
Donc, en sql, un left join sur la dernière table
 
La requête suivante semble fonctionner

Code :
  1. from ac in repository_.All<AnimalCategory>()
  2.                                   where ac.AlphaCode == alphaCode
  3.                                   select new
  4.                                   {
  5.                                       ac,
  6.                                       AnimalTypes = from at in ac.AnimalTypes
  7.                                                     select new
  8.                                                     {
  9.                                                         Animals = from a in at.Animals
  10.                                                                   where a.ProductionUnit.Id == puid
  11.                                                                   select a
  12.                                                     }
  13.                                   }).FirstOrDefault().ac;


 
Mais je me demande si on ne peut pas faire mieux, plus compact...
 
Des rois de linq ?

Reply

Marsh Posté le 20-05-2011 à 12:01:03   

Reply

Marsh Posté le 30-05-2011 à 11:54:30    

J'ai pas le temps de tester mais je serais parti sur une solution comme celle-ci

 

from ac in repository.AnimalCategory
join at in repository.AnimalType  ac equals at.AnimalCategory
join a in repository.Animal on at equals a.AnimalType into Animals
from animal in Animals.DefautIfEmpty()
select ....

 


Message édité par alien conspiracy le 30-05-2011 à 11:55:07
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed