如何使用FirstOrDefault里面包含 [英] How to use FirstOrDefault inside Include

查看:239
本文介绍了如何使用FirstOrDefault里面包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体项目和图片。
每个项目可以有多个图片,但我只想要回每个项目的一张图片时,我列出的项目。

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类

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
}

照片类

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
}

控制器

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

包含路径前pression必须引用的类型定义的导航属性。用虚线路径参考导航属性和集合导航属性选择运营商。

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.

该视图是强类型

@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>

在我看来,我想结合文件路径文件名来使用它的 IMG 的SRC。
但我不知道该怎么做。

And in my view I want to combine Filepath and Filename to use it for the img's src. But I'm not sure how to do it.

推荐答案

我假设你正在使用LINQ的实体。包括指定相关对象在查询结果中包含。选择的东西,不能直接在您​​的项目实体,如对LINQ的实体使用延迟加载时,这是非常有用的。你是我的假设后,只选择在每个项目的第一个或默认的图片。这将是这个样子:

I assume your are using Linq for Entities. Include specifies the related objects to include in the query results. This is useful when selecting something that is not directly on your Item entity, as Linq for Entities uses lazy loading. 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();

更新
正如阿诺德所说,查询实际上是预先加载的第一个项目。没有必要为.INCLUDE

Update As mentioned by Arnold, the query is actually eager loading the first item. There is no need for .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. 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; }
}

控制器

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

查看

@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>

这篇关于如何使用FirstOrDefault里面包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆