Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Lambda表达式在ICollection中调用ICollection_C#_Asp.net Mvc_Lambda - Fatal编程技术网

C# 使用Lambda表达式在ICollection中调用ICollection

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

我想要一个lambda表达式,当我从id为1030的数据库中调用一家公司以获取公司信息时,一个包含该公司所有汽车的列表和一个与每辆汽车相关的所有图像的列表,每辆汽车有4个图像

我的班级结构:

第二张给我带来了公司信息、地址和汽车清单,这是成功的一半,但不是图片

第一张给了我一张所有图片信息的列表。

我找到了解决方案

//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);