简体   繁体   English

如何在View中使用GroupBy数据-ASP.NET MVC 4

[英]How to use GroupBy Data in View - ASP.NET MVC 4

I have the following LINQ code. 我有以下LINQ代码。

public List<IGrouping<Guid, ProfileImage>> GetAllUser()
{
    return _profileImageRepository.QueryProfileImage()
        .GroupBy(g => g.UserId)
        .ToList();
}

The idea is that it will retrieve all users' profile pictures, but if a user has more than one image, only the last image to be returned. 这个想法是它将检索所有用户的个人资料图片,但是如果用户有多个图像,则仅返回最后一个图像。

I'm not sure it is right, but that's not my biggest problem. 我不确定这是对的,但这不是我最大的问题。

In my controller, I have the following code. 在我的控制器中,我有以下代码。

_homeViewModel.ProfileImages = _profileImageService.GetAllUser();

ViewModel: ViewModel:

public List<IGrouping<Guid, ProfileImage>> ProfileImages { get; set; }

My big question is, how do I use this in my view, so that I can print the correct info. 我的主要问题是,如何在我的视图中使用它,以便可以打印正确的信息。

When I look at the data in the immidiate Windows so it looks like this: 当我查看高级Windows中的数据时,它看起来像这样:

Model.ProfileImages
Count = 2
    [0]: {System.Data.Objects.ELinq.InitializerMetadata.Grouping<System.Guid,Zipr.Models.ProfileImage>}
    [1]: {System.Data.Objects.ELinq.InitializerMetadata.Grouping<System.Guid,Zipr.Models.ProfileImage>}

I have tried to do this: 我试图做到这一点:

<ul>
    @foreach (var image in Model.ProfileImages)
    {
        <li>
            <img src="@Url.Content(String.Format("~/Content/uploads/thumbs/{0}", image.ThumbPath))" alt="" />
        </li>
    }
</ul>

Anyone have a solution how I can access my Properties ThumbPath and more? 任何人都有解决方案,该如何访问我的属性ThumbPath等?

It doesn't seem you're interested in ID; 您似乎对ID不感兴趣; so change this to return a list of ProfileImage 所以更改它以返回ProfileImage的列表

public List<ProfileImage> GetAllUser()
{
    return _profileImageRepository.QueryProfileImage()
        .GroupBy(g => g.UserId)
        .Select(u => u.First() } 
        .ToList();
}

Now when your view loops over the images it will be actually getting a ProfileImage Object and not an instance of an IGrouping 现在,当您的视图遍历图像时,它将实际上得到一个ProfileImage对象,而不是IGrouping的实例。

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

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