简体   繁体   English

查询Linq加入多对多

[英]Query Linq Join Many to Many

I'm using Linq-SQL Entity for my MVC2 Application. 我正在为我的MVC2应用程序使用Linq-SQL实体。

I have those tables / entities 我有那些表/实体

Person ( ID , Name , Surname ) Car (ID , Model , Name) Reseller ( ID , Name) 人(身份证,姓名,姓氏)车(身份证号码,型号,姓名)经销商(身份证号码,姓名)

And i have those 2 Many to Many Tables 而且我有2到多个表

Persons_Cars ( ID , ID_Person , ID_CAR) Persons_Resellers ( ID, ID_Person, ID_Reseller) Persons_Cars(ID,ID_Person,ID_CAR)Persons_Resellers(ID,ID_Person,ID_Reseller)

 public MyModel {

   //Some Fields

    public List<CarPerson> carList;

    public CarPerson 
    {
      String detail;
      int id;
    }
}
 var query=(from person in data.Person

                     join PerCar in data.Persons_Cars on person.ID equals Persons_Cars.ID_Person into JoinedPerCar
                     from PerCar in JoinedPerCar.DefaultIfEmpty()

                     join car in data.car on PerCar .id_car equals car.ID into JoinedCarPe
rson
                     from car in JoinedCarPerson.DefaultIfEmpty()

                     join PerReseller in data.Persons_Resellers on person.ID equals  PerReseller .id_person into JoinedPersReseller 
                     from PerReseller in JoinedPersReseller.DefaultIfEmpty()

                     join Reseller in data.Reseller on PerReseller.id_reseller equals Reseller.ID into JoinedResellerPers
                     from Resller in JoinedFormazioneComp.DefaultIfEmpty()

                     where person.ID_USER == USER.ID 
                     select new MyModel
                                { 
                                     carList = JoinedPerCar.Select(m=>new CarPerson {detail=m.car.Model,id = m.ID}).ToList()},
                                //Other
                                });

I know that it's totally wrong but i'm new into Linq-SQL I have a problem now : 我知道这是完全错误的,但我是Linq-SQL的新手我现在遇到了一个问题:

The Query works but my result is wrong. 查询有效但我的结果是错误的。 Infact if a person has 2 cars i recive the same person two times with 2 list of the cars. 事实上,如果一个人有2辆车,我会用2个车辆清单两次收回同一个人。

Example

Mr Brown has 2 Cars i Recive Mr Brown ---> List of car(Car1 , Car2) Mr Brown ---> List of car(Car1, Car2) 布朗先生有2辆汽车我回收布朗先生--->汽车清单(Car1,Car2)布朗先生--->汽车清单(Car1,Car2)

So i need that if a person has 2 car i get only 1 result of person with his own cars 所以我需要的是,如果一个人有2辆车我只得到1个人用他自己的车的结果

Is there a way to write this query better ? 有没有办法更好地编写此查询? How could resolve my problem? 怎么能解决我的问题?

Thanks 谢谢

I think simple subquery will do the job (same for resellers): 我认为简单的子查询将完成这项工作(对于经销商而言):

from p in data.Person
select new MyModel {
  carList = (from pc in data.Persons_Cars
             join c in data.car on pc.id_car equals c.ID
             where pc.ID_Person == p.ID 
             select c).ToList()
};

If you have navigation properties defined, then query will be even more simple (Linq will do join for you): 如果您定义了导航属性,那么查询将更加简单(Linq将为您加入):

from p in data.Person
select new MyModel {
   carList = p.Person_Cars.Select(pc => pc.Car).ToList()
};

With the right properties on Person and Car and database relationships, this can be setup in the OnModelCreating method of your context so you don't need a Person_Cars model in your context: 使用Person和Car上的正确属性以及数据库关系,可以在上下文的OnModelCreating方法中进行设置,这样您在上下文中就不需要Person_Cars模型:

modelBuilder.Entity<Car>()
                .HasMany(x => x.People)
                .WithMany(y => y.Cars)
                .Map(z =>
                {
                    z.MapLeftKey("CarID");
                    z.MapRightKey("PersonID");
                    z.ToTable("Person_Cars");
                });

Then you should be able to get your Person's Cars via: 那么你应该能够通过以下方式获得你的人车:

from p in context.People
where p.PersonID = pID
select p.Cars

Or return the Person with their populated Car list: 或者使用填充的Car列表返回Person:

context.People.Where(p => p.PersonID == pID).Include(c => c.Cars).FirstOrDefault();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM