日期:2014-05-20  浏览次数:20728 次

linq to sql联接映射问题,帮忙!!!
有3个泛型集合 我想通过linq to sql的联接后映射出新的集合
其中Employee类中有字段(Id,Name,Hiredate,Birthday,Sex,Telephone,Address)
  UseVehicle类中有字段(Id,EmployeeId,StartTime,EndTime,Destination,Cause);
  Vehicle类中有字段(Id,NumberPlate,Description,Brand,Model);
  employee中的id和UseVehicle中的EmployeeId是主外  
   
 List<UseVehicle> u = uvb.GetUseVehicles().ToList();
 List<Employee> e = vb.GetEmployees().ToList();
 List<Vehicle> v = vb.GetVehicle().ToList();

我想把上面的3个集合通过linq to sql的连接后映射出一个新集合,
其中新集合中属性包含(Vehicle中NumberPlate,Employee中的Name,UseVehicle中的StartTime,endTime,Destination,Cause)

var NewList=?

帮帮忙!

------解决方案--------------------
这样是不是要先建一个类啊,把三个表相关的字段添加进去?或者可以先在数库里生成视图。然后用linq查视图。

------解决方案--------------------
三表查询 inner join or left join
最后 select new {} 一下
------解决方案--------------------
Vehicle 和其他两张表的关系是?

你可以先写出SQL语句,我帮你翻译成LINQ
------解决方案--------------------
C# code

List<UseVehicle> u = uvb.GetUseVehicles().ToList();
 List<Employee> e = vb.GetEmployees().ToList();
 List<Vehicle> v = vb.GetVehicle().ToList();

var q=from c1 in u join c2 in e on c1.id equals c2.EmployeeId join c3 in v on  c3.id equals c1.VehicleId select new{zd1=c1.NumberPlate,zd2=....}

------解决方案--------------------
探讨

引用:
select v.NumberPlate,e.Name,uv.Id,uv.StrartTime,uv.EndTime,uv.Destination,uv.Cause from Vehicle v,Employee e,UseVehicle uv
where v.Id=uv.VehicleId and uv.EmployeeId=e.Id;

------解决方案--------------------
探讨
Vehicle 和其他两张表的关系是?

你可以先写出SQL语句,我帮你翻译成LINQ