C# 实体框架 ORDER BY 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14593426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Entity Framework ORDER BY issue
提问by Dakine83
I'm attempting to build my first MVC 4 application using the entity framework. All I'm looking for is to create a drop down list with the value and text of each option set to the same value.
我正在尝试使用实体框架构建我的第一个 MVC 4 应用程序。我正在寻找的是创建一个下拉列表,其中每个选项的值和文本都设置为相同的值。
This works, right up until I throw in the GroupBy()
.
这有效,直到我将GroupBy()
.
Create.cshtml
创建.cshtml
@Html.DropDownList("CustomerName",(SelectList)ViewData["companies"]);
ticketController.cs
票务控制器.cs
ViewBag.companies = new SelectList(oc_db.company.Where(c => c.status == "ACTIVE")
.OrderBy(c => c.name_1)
.GroupBy(c=>c.name_1)
, "name_1", "name_1");
Here is the Error I'm receiving:
这是我收到的错误:
DataBinding: 'System.Data.Objects.ELinq.InitializerMetadata+Grouping`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[OpsTicketing.Models.company, OpsTicketing, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' does not contain a property with the name 'name_1'.
数据绑定:'System.Data.Objects.ELinq.InitializerMetadata+Grouping`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[OpsTicketing.Models.company, OpsTicketing= 1.0.0.0, Culture=neutral, PublicKeyToken=null]]' 不包含名为 'name_1' 的属性。
If I don't use the GroupBy
the query works, albeit with duplicates.
如果我不使用GroupBy
查询工作,尽管有重复。
采纳答案by D Stanley
GroupBy
doesn't give you an enumeration of the underlying type. It gives you an enumeration of IGrouping
objects with a Key
field that gives you the key value for that group and an IEnumerable
interface that lets you iterate the members of that group.
GroupBy
不会为您提供基础类型的枚举。它为您提供了一个IGrouping
对象枚举,其中一个Key
字段为您提供该组的键值,以及一个IEnumerable
让您迭代该组成员的界面。
If all you want is a unique list of name_1
values in order, just select that field and do a Distinct
followed by an OrderBy
:
如果您想要的name_1
只是按顺序排列的唯一值列表,只需选择该字段并执行 aDistinct
后跟OrderBy
:
ViewBag.companies = new SelectList(oc_db.company.Where(c => c.status == "ACTIVE")
.Select(c=> new {c.name_1})
.Distinct()
.OrderBy(c => c.name_1)
, "name_1", "name_1");
Note that you coulddo it without the .Select()
, but you'd have to define "equality" for your company
class, which is more trouble than it's worth for this exercise. That's why the Distinct
didn't work before - because you didn't define what makes two companies distinct.
请注意,您可以在没有 的情况下执行此操作.Select()
,但是您必须为您的company
班级定义“平等” ,这比本练习的价值要麻烦。这就是为什么Distinct
以前不起作用的原因-因为您没有定义使两家公司不同的原因。
回答by mattytommo
You'll have to resolve the query before performing the GroupBy, which you can do by calling .ToList()
您必须在执行 GroupBy 之前解决查询,您可以通过调用 .ToList()
回答by Scott Isaacs
Are you using the grouping just to make the list distinct? If so, try:
您是否使用分组只是为了使列表不同?如果是这样,请尝试:
.Where(c => c.status == "ACTIVE")
.OrderBy(c => c.name_1)
.Distinct()
回答by Chen
How about using .Distinct()? http://msdn.microsoft.com/en-us/library/bb348436.aspx
使用 .Distinct() 怎么样? http://msdn.microsoft.com/en-us/library/bb348436.aspx
Should work if this is all you need.
如果这就是你所需要的,应该可以工作。