简体   繁体   English

如何在Include中使用FirstOrDefault

[英]How to use FirstOrDefault inside Include

I have two entities for items and pictures. 我有两个实体用于物品和图片。 Each item can have multiple pictures, but I only want to return one picture of each item when I list the items. 每个项目可以有多张图片,但是当我列出这些项目时,我只想返回一张图片。

Item class 物品类别

public class Item 
{
    [Key]
    public int ItemID { get; set; }
    public string Name{ get; set; }
    public string Status{ get; set; }
    public virtual ICollection<Picture> Pictures{ get; set; } // Navigation property
}

Picture class 图片类

public class Picture
{
    [Key]
    public int PictureID { get; set; }
    public string Filename{ get; set; }
    public string Filepath{ get; set; }
    public int ItemID { get; set; } // Foreign Key
    public virtual Item Item{ get; set; } // Navigation property
}

Controller 调节器

public ActionResult Lager()
{
    var model = _db.Items.Include(b => b.Pictures.FirstOrDefault()).Where(i =>
    i.Status ==  0)

    return View(model);
}

I get this error from my controller 我从控制器收到此错误

The Include path expression must refer to a navigation property defined on the type. 包含路径表达式必须引用在类型上定义的导航属性。 Use dotted paths for reference navigation properties and the Select operator for collection navigation properties. 使用虚线路径作为参考导航属性,使用“选择”运算符作为集合导航属性。

The view is strongly typed 视图是强类型的

@model IEnumerable<MyProject.Models.Koeretoej>

<div">
@foreach(var item in Model){
    <ul>
        <li>
            <span class="icon">
                <img src="@item.Pictures.Filepath@item.Billeder.Filepath"/>
            </span>
        </li>
        <li><span class="text">@item.Name</span></li>
        <li><span class="text">@item.Status</span></li>
    </ul>
}
</div>

And in my view I want to combine Filepath and Filename to use it for the img 's src. 在我看来,我想结合使用FilepathFilename来将其用于img的src。 But I'm not sure how to do it. 但是我不确定该怎么做。

I assume your are using Linq for Entities. 我假设您正在使用Linq for Entities。 Include specifies the related objects to include in the query results. Include指定要包含在查询结果中的相关对象。 This is useful when selecting something that is not directly on your Item entity, as Linq for Entities uses lazy loading. 当选择不直接位于Item实体上的内容时,这很有用,因为LINQ for Entities使用延迟加载。 What i assume you are after, is only Selecting the first or default Picture on each of your Item. 我认为您所追求的只是在每个项目上选择第一张或默认图片。 That would look something like this: 看起来像这样:

_db.Items.Where(i => i.Status == 0)
         .Select(i => new { Picture = i.Pictures.FirstOrDefault(), Item = i} )
         .ToList();

Update As mentioned by Arnold, the query is actually eager loading the first item. 更新正如Arnold所提到的,查询实际上渴望加载第一项。 There is no need for .Include. 不需要.include。

Update according to updated question Generally, it's better design to not pass all kinds of unnecessary thing to the view. 根据更新的问题进行更新通常,最好不要将各种不必要的东西传递给视图。 Also, the query above creates an anonymous object in the select, that can't be passed to a view. 同样,上面的查询在select中创建一个匿名对象,该对象无法传递给视图。 Instead, make a specific model for your view. 而是,为您的视图创建一个特定的模型。 Perhaps like this: 也许像这样:

ItemWithPicture.cs ItemWithPicture.cs

public class ItemWithPicture 
{
    public int ItemID { get; set; }
    public string Name{ get; set; }
    public string Status{ get; set; }
    public string PictureFilename{ get; set; }
    public string PictureFilepath{ get; set; }
}

Controller 调节器

public ActionResult Lager()
{
    var model = _db.Items.Where(i => i.Status == 0).Select(i => new { Picture = i.Pictures.FirstOrDefault(), Item = i} ).Select(i => new {
              ItemID = i.Item.ItemID,
              Name =  i.Item.Name,
              Status = i.Item.Status,
              PictureFilename = i.Picture != null ? i.Picture.Filename : null,
              PictureFilepath = i.Picture != null ? i.Picture.Filepath : null
         }).ToList();
    return View(model);
}

View 视图

@model IEnumerable<SomeNamespace.ItemWithPicture>

<div>
@foreach(var item in Model){
    <ul>
        <li>
            <span class="icon">
                <img src="@item.Filepath"/>
            </span>
        </li>
        <li><span class="text">@item.Name</span></li>
        <li><span class="text">@item.Status</span></li>
    </ul>
}
</div>

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

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