我从数据库中挑选名单,根据情况,每个名字在数据库中存在很多次,没关系。现在我已经用这些名称填充了DROPDOWNLIST,但在下拉列表中,每个名称都出现了几次:
但我想在DROPDOWN中显示每个名字一次。我使用了distict()但没有工作。
使用MVC 3,Linq to SQL
控制器:
namespace EmployeeAttendance_app.Controllers
{
public class HomeController : Controller
{
EmployeeAtdDataContext DataContext = new EmployeeAtdDataContext();
public ActionResult Index()
{
ViewBag.Message = "Precise Technology Consultants";
//var EmployeeAtd = DataContext.GetAttendance_Sp();
IEnumerable<GetAtdRecord_SpResult> EmployeeAtd = DataContext.GetAtdRecord_Sp(null).ToList();
var names = (from n in DataContext.EmployeeAtds select n).Distinct();
//ViewData["EmplID"] = new SelectList(names, "EmplID", "EmplName");
return View(EmployeeAtd);
}
public ActionResult ddl()
{
var names = (from n in DataContext.EmployeeAtds select n).Distinct();
ViewData["EmplID"] = new SelectList(names, "EmplID", "EmplName");
return View();
}
public ActionResult showDDL(string EmplID)
{
ViewBag.EmplID = EmplID;
return View();
}
public ActionResult About()
{
return View();
}
查看:
@{
ViewBag.Title = "ddl";
}
<h2>ddl</h2>
@using (Html.BeginForm("showDDL", "Home", FormMethod.Get))
{
<fieldset>
Employers
@Html.DropDownList("EmplID", "Select Name")
<p>
<input type="submit" value="Submit" />
</p>
</fieldset>
}
模型
amespace EmployeeAttendance_app.Models
{
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;
using System.ComponentModel;
using System;
[global::System.Data.Linq.Mapping.DatabaseAttribute(Name="a1")]
public partial class EmployeeAtdDataContext : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
#region Extensibility Method Definitions
partial void OnCreated();
#endregion
public EmployeeAtdDataContext() :
base(global::System.Configuration.ConfigurationManager.ConnectionStrings["a1ConnectionString"].ConnectionString, mappingSource)
{
OnCreated();
}
public EmployeeAtdDataContext(string connection) :
base(connection, mappingSource)
{
OnCreated();
}
public EmployeeAtdDataContext(System.Data.IDbConnection connection) :
base(connection, mappingSource)
{
OnCreated();
}
public EmployeeAtdDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public EmployeeAtdDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public System.Data.Linq.Table<EmployeeAtd> EmployeeAtds
{
get
{
return this.GetTable<EmployeeAtd>();
}
}
[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.GetAtdRecord_Sp")]
public ISingleResult<GetAtdRecord_SpResult> GetAtdRecord_Sp([global::System.Data.Linq.Mapping.ParameterAttribute(Name="EmplID", DbType="Int")] System.Nullable<int> emplID)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), emplID);
return ((ISingleResult<GetAtdRecord_SpResult>)(result.ReturnValue));
}
}
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.V_EmployeeAtd")]
public partial class EmployeeAtd
{
private string _EmplID;
private string _EmplName;
private string _RecDate;
private string _RecTime;
private string _DeptName;
public EmployeeAtd()
{
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_EmplID", DbType="Char(8) NOT NULL", CanBeNull=false)]
public string EmplID
{
get
{
return this._EmplID;
}
set
{
if ((this._EmplID != value))
{
this._EmplID = value;
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_EmplName", DbType="NVarChar(40) NOT NULL", CanBeNull=false)]
public string EmplName
{
get
{
return this._EmplName;
}
set
{
if ((this._EmplName != value))
{
this._EmplName = value;
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_RecDate", DbType="Char(10)")]
public string RecDate
{
get
{
return this._RecDate;
}
set
{
if ((this._RecDate != value))
{
this._RecDate = value;
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_RecTime", DbType="Char(5)")]
public string RecTime
{
get
{
return this._RecTime;
}
set
{
if ((this._RecTime != value))
{
this._RecTime = value;
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_DeptName", DbType="NVarChar(50)")]
public string DeptName
{
get
{
return this._DeptName;
}
set
{
if ((this._DeptName != value))
{
this._DeptName = value;
}
}
}
}
public partial class GetAtdRecord_SpResult
{
private string _EmplID;
private string _EmplName;
private string _InTime;
private string _OutTime;
private string _DateVisited;
private string _TimeWorked;
private string _OverTime;
public GetAtdRecord_SpResult()
{
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_EmplID", DbType="Char(8) NOT NULL", CanBeNull=false)]
public string EmplID
{
get
{
return this._EmplID;
}
set
{
if ((this._EmplID != value))
{
this._EmplID = value;
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_EmplName", DbType="NVarChar(40) NOT NULL", CanBeNull=false)]
public string EmplName
{
get
{
return this._EmplName;
}
set
{
if ((this._EmplName != value))
{
this._EmplName = value;
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_InTime", DbType="Char(5)")]
public string InTime
{
get
{
return this._InTime;
}
set
{
if ((this._InTime != value))
{
this._InTime = value;
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_OutTime", DbType="Char(5)")]
public string OutTime
{
get
{
return this._OutTime;
}
set
{
if ((this._OutTime != value))
{
this._OutTime = value;
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_DateVisited", DbType="Char(10) NOT NULL", CanBeNull=false)]
public string DateVisited
{
get
{
return this._DateVisited;
}
set
{
if ((this._DateVisited != value))
{
this._DateVisited = value;
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_TimeWorked", DbType="Char(5)")]
public string TimeWorked
{
get
{
return this._TimeWorked;
}
set
{
if ((this._TimeWorked != value))
{
this._TimeWorked = value;
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_OverTime", DbType="VarChar(6)")]
public string OverTime
{
get
{
return this._OverTime;
}
set
{
if ((this._OverTime != value))
{
this._OverTime = value;
}
}
}
}
}
答案 0 :(得分:1)
您需要确保您的基础对象覆盖GetHashCode
和Equals
,以使其正常工作。
请参阅此处了解更多信息:
http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx
例如,如果您有一个名为EmployeeAtd
的对象,则需要实现上述两种方法,以便了解比较对象何时相等。
这是一个简单的示例,但如果它们具有匹配的EmplID,您可以认为EmployeeAtd
个对象是相同的,例如,等于覆盖可以是:
public override bool Equals(object obj)
{
if (obj == null) return false;
if (this.GetType() != obj.GetType()) return false;
EmployeeAtd emp = (EmployeeAtd) obj;
if (!Object.Equals(EmplID, emp.EmplID)) return false;
return true;
}
如果您的EmplID
是int,则可以按如下方式实现GetHashCode
:
public override int GetHashCode ()
{
return EmplID.GetHashCode();
}
这不是最佳做法,但还有很多关于如何做到这一点的其他问题。
What is the best way to implement this composite GetHashCode()
Why is it important to override GetHashCode when Equals method is overridden?
答案 1 :(得分:1)
您可以安全地使用DISTINCT,我不确定您在哪里遇到问题。请按照这个例子。说我有这样一个数据库表 -
然后我按照存储过程编写
USE [Sample]
GO
CREATE PROCEDURE GiveNames
AS
SELECT DISTINCT(Name) FROM [dbo].[SampleTable]
GO
然后我创建一个MVC项目并添加EDMX并以下列方式使用Controller中的内容 -
public class EdmxController : Controller
{
//
// GET: /Edmx/
public ActionResult Index()
{
DDLModel model = new DDLModel();
model.Items = new List<string>();
using (var entities = new SampleEntities1())
{
model.Items = entities.GiveNames().ToList();
}
return View(model);
}
}
public class DDLModel
{
public List<String> Items { get; set; }
}
我的观点 -
@model MVC.Controllers.DDLModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<div>
@Html.DropDownList("MyDDL",new SelectList(Model.Items), "--Choose any Item--")
</div>
我的输出没有重复 -
答案 2 :(得分:1)
我认为你在n上实现了distinct,它包含了class的所有属性。您只需要在必需的列上应用distinct。像
var empList= DataContext.EmployeeAtds.Select(n => new
{
EmplID= n.EmplID,
EmplName= n.EmplName
}).Distinct().ToList();
OR
var empList= (from n in DataContext.EmployeeAtds select new{ EmplID= n.EmplID,
EmplName= n.EmplName}).Distinct();