使用Linq对项目进行分组并在MVC视图的表中显示 [英] Group items with Linq and display in table in MVC view

查看:75
本文介绍了使用Linq对项目进行分组并在MVC视图的表中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够显示按类别分组的数据库中的数据.该应用程序是时间表应用程序的尝试.数据库模式如下所示:

I want to be able to display data from a database grouped under categories. The application is an attempt at a timesheet application. The database schema looks like this:

(table) TimeSegments
TimeSegmentID
Hours
Date
ConsultantID (FK)
TaskID (FK)
ProjectID (FK)

(table) Projects
ProjectID
ProjectName

(table) Tasks
TaskID
TaskName

(table) Consultants
ConsultantID
ConsultantName

我可以从模型(这里称为ts)中获取数据并显示它,但是我希望将其分组以便可以按项目和任务显示小时数.到目前为止,这是我的尝试.我猜想Linq中的group语句是合适的,但是我不知道这是否正确:

I can get the data from the model (here called ts) and display it, but I want it to be grouped so that I can show hours by project and task. Here's my attempt so far. I guessed the group statement in Linq would be appropriate, but I don't know if that's correct:

    public IList<TimeSegment> TimeSegmentsByProject
    {
        get
        {
            var projectGroups = from timeSegment in ts.TimeSegments
                                group timeSegment by timeSegment.Project
                                into projectGroup
                                select projectGroup;
            return projectGroups.ToList(); //Doesn't work
        }
    }

现在这失败了,因为根据编译器无法将结果转换为列表.我以为这个表达式可以给我一个集合的集合,并且我可以返回它,将其放入控制器中,并将其传递给视图进行显示.显然不是.

Now this fails, because the result can't be converted to a list according to the compiler. I thought this expression would give me a collection of collections, and that I would be able to return this, get it in the controller, and pass it to the view for display. Apparently not.

那么我应该怎么做呢?如果有人可以告诉我如何返回正确的类型,并且最好还给我一个示例,说明视图如何接收并显示按Project分类的视图,我将不胜感激!

So how should I go about this? If anyone could show me how to return the correct type, and preferably also give me an example of how a view could receive it and display it categorized by Project, I would really appreciate it!

这也不起作用(以下The_Smallest建议的返回类型):

This doesn't work either (return type suggested by The_Smallest below):

    public IGrouping<Project, TimeSegment> TimeSegmentsByProject
    {
        get
        {
            var projectGroups = from timeSegment in ts.TimeSegments
                                group timeSegment by timeSegment.Project
                                into projectGroup
                                select projectGroup;
            return projectGroups;
        }
    }

编译器表示无法将[blabla] IQueryable转换为此类型(类型相似,但返回类型为IQueryable)

The compiler says it cannot convert [blabla] IQueryable to this type (the types are similar, but the return type is IQueryable)

推荐答案

group-by命令后,linq返回IEnumerable<IGrouping<TKey, TValue>>,而您希望它是IEnumerable<TimeSegment>.
在您的示例中,您可以从IList<TimeSegment>更改方法的返回类型 到IEnumerable<IGrouping<Project, TimeSegment>>的位置,其中Project是timeSegment.Project的类型
PS:您可以将IEnumerable更改为Array或IList(根据需要,只要不要忘记添加.ToArray().ToList())

After group-by command linq returns IEnumerable<IGrouping<TKey, TValue>>, while you expect it to be IEnumerable<TimeSegment>.
In your example you can change return type of your method from IList<TimeSegment> to IEnumerable<IGrouping<Project, TimeSegment>> where Project is type of timeSegment.Project
PS: You can change IEnumerable to Array or IList (Whichever you want, just don't forget to Add .ToArray() or .ToList())

这篇关于使用Linq对项目进行分组并在MVC视图的表中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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