C# 使用Lambda表达式在ICollection中调用ICollection
我想要一个lambda表达式,当我从id为1030的数据库中调用一家公司以获取公司信息时,一个包含该公司所有汽车的列表和一个与每辆汽车相关的所有图像的列表,每辆汽车有4个图像 我的班级结构: 第二张给我带来了公司信息、地址和汽车清单,这是成功的一半,但不是图片 第一张给了我一张所有图片信息的列表。我找到了解决方案C# 使用Lambda表达式在ICollection中调用ICollection,c#,asp.net-mvc,lambda,C#,Asp.net Mvc,Lambda,我想要一个lambda表达式,当我从id为1030的数据库中调用一家公司以获取公司信息时,一个包含该公司所有汽车的列表和一个与每辆汽车相关的所有图像的列表,每辆汽车有4个图像 我的班级结构: 第二张给我带来了公司信息、地址和汽车清单,这是成功的一半,但不是图片 第一张给了我一张所有图片信息的列表。我找到了解决方案 //Get the Company with id == 1030. var x = _context.Companies.Include(a => a.AddressN
//Get the Company with id == 1030.
var x = _context.Companies.Include(a => a.AddressNavigation).Include(c => c.Cars).Where(c => c.CompanyId == id).FirstOrDefault();
//Get the Car ICollection and include the CarImages ICollection depending on CompanyID.
var y = _context.Cars.Include(img => img.CarImages).Where(cr => cr.CompanyId == x.CompanyId).ToList();
//Now each Car in our Cars list has a list of CarImages related to each car. Finaly pass our data to our Company var(x) and send it to View.
x.Cars = y;
return View(x);
不要张贴文字图片。请编辑您的问题以使用实际文本。只需将代码直接复制粘贴到问题中即可。
var compInfo = _context.Companies.SelectMany(cr => cr.Cars.SelectMany(i =>i.CarImages.Where(car => car.CarId == car.Car.CarId))).ToList();
var compInfo2 = _context.Companies.Include(a => a.AddressNavigation).Include(cr =>cr.Cars.).FirstOrDefault(c => c.CompanyId == id);
//Get the Company with id == 1030.
var x = _context.Companies.Include(a => a.AddressNavigation).Include(c => c.Cars).Where(c => c.CompanyId == id).FirstOrDefault();
//Get the Car ICollection and include the CarImages ICollection depending on CompanyID.
var y = _context.Cars.Include(img => img.CarImages).Where(cr => cr.CompanyId == x.CompanyId).ToList();
//Now each Car in our Cars list has a list of CarImages related to each car. Finaly pass our data to our Company var(x) and send it to View.
x.Cars = y;
return View(x);