简体   繁体   English

返回LINQ查询以查看并循环查看视图中的结果

[英]return LINQ query to view and loop through the results in the view

I am running in to a problem where I have made the following query in my controller: 我遇到了一个问题,我在我的控制器中进行了以下查询:

var query = from pmt in db.ProjectHasTags
            join project in db.Projects on pmt.ProjectId equals project.ID
            join tag in db.ProjectTags
                 on pmt.TagId equals tag.ID
                 group pmt by pmt.Project into pmtGroup
                    select new
                    {
                        Project = pmtGroup.Key,
                        Tags = pmtGroup.Select(project => project.ProjectTag)
                    };

I want to return this query to a view using: 我想使用以下命令将此查询返回到视图:

return View(query.ToList());

In the view file I have the following code: 在视图文件中,我有以下代码:

@model IEnumerable<portfolio.Models.ProjectHasTag>

@foreach (var p in Model)
{
    @p.Project.Title

    foreach (var tag in p.Tags)
    {
        @tag.title
    }       
}

I get the following error: 我收到以下错误:

The model item passed into the dictionary is of type 'System.Collections.Generic.List 1[<>f__AnonymousType6 2[portfolio.Models.Project,System.Collections.Generic.IEnumerable 1[portfolio.Models.ProjectTag]]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1[portfolio.Models.ProjectHasTag]'. 传递到字典中的模型项的类型为'System.Collections.Generic.List 1[<>f__AnonymousType6 2 [portfolio.Models.Project,System.Collections.Generic.IEnumerable 1[portfolio.Models.ProjectTag]]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1 [portfolio.Models.ProjectHasTag]' 1[portfolio.Models.ProjectTag]]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable

The ProjectHasTag model code: ProjectHasTag模型代码:

public class ProjectHasTag
{
    public int ID { get; set; }

    public int? ProjectId { get; set; }

    [ForeignKey("ProjectId")]
    [DisplayName("Project")]
    public virtual Project Project { get; set; }

    public int? TagId { get; set; }

    [ForeignKey("TagId")]
    [DisplayName("Tag")]
    public virtual ProjectTag ProjectTag { get; set; }

    public virtual ICollection<ProjectTag> Tags { get; set; }

}

This is what I want to achieve: http://i.stack.imgur.com/DAZ5n.png (I cant post images yet) 这就是我想要实现的目的: http//i.stack.imgur.com/DAZ5n.png (我无法发布图片)

thanks for taking the time, English is not my first language. 感谢花时间,英语不是我的第一语言。

The problem is this part of your query: 问题是这部分查询:

select new
{
    Project = pmtGroup.Key,
    Tags = pmtGroup.Select(project => project.ProjectTag)
};

You're not specifying the type that should be instantiated, so it's creating an anonymous type, which is then being passed to your view. 您没有指定应该实例化的类型,因此它创建了一个匿名类型,然后将其传递给您的视图。 You probably want something like this: 你可能想要这样的东西:

select new ProjectHasTag
{
    Project = pmtGroup.Key,
    Tags = pmtGroup.Select(project => project.ProjectTag)
};

Update 更新

Just as the error is telling you, ProjectHasTag does not have a Tags property. 就像错误告诉你的那样, ProjectHasTag没有Tags属性。 It looks like what you want is really this: 它看起来像你想要的是这样的:

select new ProjectHasTag
{
    Project = pmtGroup.Key,
    ProjectTag = pmtGroup.Select(project => project.ProjectTag)
};

However, it's a little unclear what you're trying to do because in your view it looks as though you have multiple tags for each project, in which case it should really be a collection. 但是,你有点不清楚你想要做什么,因为在你的视图中看起来好像你有每个项目的多个标签,在这种情况下它应该真的是一个集合。 Something like: 就像是:

public virtual ICollection<ProjectTag> Tags { get; set; }

Update Two 更新二

I forgot the Entity Framework (EF) is picky when it comes to directly instantiating entity types. 我忘了实体框架(EF)在直接实例化实体类型时很挑剔。 For a quick fix, you should be able to map from an anonymous type to the entity type (as described here ). 为了速战速决,你应该能够从一个匿名类型的实体类型映射(如描述在这里 )。

However, EF doing this is actually a good thing because it's forcing you to adopt a design strategy that will allow you to leverage more power from MVC. 然而,EF这样做实际上是一件好事,因为它迫使你采用一种设计策略,让你可以利用MVC的更多功能。 In particular, this would be a good time to learn about ViewModels (see: ASP.NET MVC - How exactly to use View Models and http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-view-model-patterns.aspx ). 特别是,这将是了解ViewModel的好时机(请参阅: ASP.NET MVC - 如何使用View Modelshttp://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net- mvc-view-model-patterns.aspx )。

In the select part of your query, you create an anonymous object, and your model requires objects form type ProjectHasTag . 在查询的选择部分中,您将创建一个匿名对象,并且您的模型需要表单类型为ProjectHasTag对象。 So this should look like (look at the select new ProjectHasTag ): 所以这应该是这样的(看看select new ProjectHasTag ):

var query = from pmt in db.ProjectHasTags
            join project in db.Projects on pmt.ProjectId equals project.ID
            join tag in db.ProjectTags
                 on pmt.TagId equals tag.ID
                 group pmt by pmt.Project into pmtGroup
                    select new ProjectHasTag
                    {
                        Project = pmtGroup.Key,
                        Tags = pmtGroup.Select(project => project.ProjectTag)
                    };

Since you have an ICollection , you are telling Linq, that you want your ProjectTags to be lazy loaded. 由于你有一个ICollection,你告诉Linq,你希望你的ProjectTag是延迟加载的。 Like @John H said above, since you have added the Tags property to your ProjectHasTag class, you can populate it such: 就像上面提到的@John H一样,既然你已经将Tags属性添加到了ProjectHasTag类中,你就可以填充它:

select new ProjectHasTag
{
    Project = pmtGroup.Key,
    Tags = pmtGroup.Select(project => project.ProjectTag)
};

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

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