[英]ASP.NET MVC LINQ Grouping
I am trying to group multiple items according to one column from my database table. 我试图根据数据库表中的一列对多个项目进行分组。 I have combed through countless posts covering this topic and have had no luck in solving my issue. 我浏览了无数有关该主题的帖子,也没有运气解决我的问题。
This is the tutorial that I am following that does exactly what I need: http://ole.michelsen.dk/blog/grouping-data-with-linq-and-mvc/ 这是我正在遵循的教程,完全可以满足我的需要: http : //ole.michelsen.dk/blog/grouping-data-with-linq-and-mvc/
This is what my Table Definition looks like: 这是我的表定义的样子:
|task_no(PK)|task_statement|field_name|
Here is an example of what I am trying to get: 这是我尝试获得的示例:
field_name 1
task_no | task_statement
task_no | task_statement
task_no | task_statement
field_name 2
task_no | task_statement
task_no | task_statement
field_name 3
task_no | task_statement
task_no | task_statement
Here are my models: 这是我的模型:
public partial class domain_names_tasks
{
[Key]public int task_no { get; set; }
public string task_statement { get; set; }
public string field_name { get; set; }
}
class created from following tutorial. 从以下教程创建的类。
public class Group<T, K>
{
public K Key;
public IEnumerable<T> Values;
}
Here is my controller: 这是我的控制器:
public ActionResult DomainTaskStatements()
{
var taskStatements = context.domain_names_tasks.Select(ts => new domain_names_tasks
{
task_no = ts.task_no,
task_statement = ts.task_statement,
field_name = ts.field_name
}).ToList();
var data = from ts in taskStatements
group ts by ts.field_name into tsg
select new Group<string, domain_names_tasks> { Key = tsg.Key, Values = tsg };
return View(data.toList());
}
I am getting two errors: 我遇到两个错误:
1st Error 第一个错误
Cannot implicitly convert type 'string' to 'GeologyWebApp.Models.domain_names_tasks'
which is being caused here: 这是在这里引起的:
Key = tsg.Key
2nd Error 第二次错误
Cannot implicitly convert type 'System.Linq.IGrouping<string,GeologyWebApp.Models.domain_names_tasks>' to 'System.Collections.Generic.IEnumerable<string>'. An explicit conversion exists (are you missing a cast?)
being caused here: 在这里引起的:
Values = tsg
I am new to using LINQ and ASP.NET so I apologize for asking a question about an issue that may be simple to solve. 我是使用LINQ和ASP.NET的新手,所以我很抱歉提出一个可能很容易解决的问题。 I have had no luck looking though other questions related to LINQ grouping so I thought I would post this question. 尽管我没有其他与LINQ分组有关的问题,但我没有运气,所以我想我会发布这个问题。 Thanks in advance! 提前致谢!
Edit 1 ------- 编辑1 -------
Sorry I did not notice my silly mistake with the Key and Value parameters in the Group class. 抱歉,我没有注意到Group类中Key和Value参数的愚蠢错误。
Here is my revised controller and view that now work. 这是我修改后的控制器,现在可以使用。
Controller: 控制器:
NOTE: I had to change my "taskStatements" query because I was getting the error: "The entity cannot be constructed in a LINQ to Entities query". 注意:我不得不更改我的“ taskStatements”查询,因为我收到错误:“该实体不能在LINQ to Entities查询中构造”。 I followed an answer found here to fix this issue. 我按照此处找到的答案来解决此问题。
public ActionResult DomainTaskStatements()
{
var taskStatements = (from ts in context.Set<domain_names_tasks>()
select new
{
task_no = ts.task_no,
task_statement = ts.task_statement,
field_name = ts.field_name
}).ToList().Select(x => new domain_names_tasks { task_no = x.task_no, task_statement = x.task_statement, field_name = x.field_name });
var data = from ts in taskStatements
group ts by ts.field_name into tsg
select new Group<string, domain_names_tasks> { Key = tsg.Key, Values = tsg };
return View(data.ToList());
}
View: 视图:
@model IEnumerable<GeologyWebApp.Models.Group<string, GeologyWebApp.Models.domain_names_tasks>>
@{
ViewBag.Title = "DomainTaskStatements";
}
<table>
<thead><tr><th>task no</th><th>task statement</th></tr></thead>
<tbody>
@foreach (var group in Model)
{
<tr><th colspan="2">@group.Key</th></tr>
foreach (var task in group.Values)
{
<tr><td>@task.task_no</td><td>@task.task_statement</td></tr>
}
}
</tbody>
</table>
select new Group<string, domain_names_tasks> { Key = tsg.Key, Values = tsg };
should probably be 应该是
select new Group<domain_names_tasks, string> { Key = tsg.Key, Values = tsg };
because on 因为在
Group<T, K>
the key (K) is the second type parameter and the type (T) is the first. 键(K)是第二个类型参数,类型(T)是第一个类型参数。
However, I don't believe you need to have your own Group type, using 'group by' in LINQ should return: 但是,我不认为您需要拥有自己的组类型,在LINQ中使用'group by'应该返回:
IEnumerable<IGrouping<K,T>>
You could probably get away with this in the controller: 您可能可以在控制器中解决这个问题:
var data = from ts in taskStatements
group ts by ts.field_name;
and then this in the view (for the nested loop): 然后在视图中(对于嵌套循环):
foreach (var task in group)
instead of 代替
foreach (var task in group.Values)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.