[英]Drop down shows each record several times
我從數據庫中挑選名單,根據情況,每個名字在數據庫中存在很多次,沒關系。 現在我已經用這些名稱填充了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;
}
}
}
}
}
您需要確保您的基礎對象重寫GetHashCode
和Equals
,以使其正常工作。
請在此處查看更多信息:
http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx
例如,如果您有一個名為EmployeeAtd
的對象,則需要實現上述兩種方法,以便了解比較對象何時相等。
這是一個簡單的示例,但如果它們具有匹配的EmplID,您可以認為EmployeeAtd
對象相同,例如Equals覆蓋可以是:
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();
}
這絕不是最佳做法,但還有很多關於如何做到這一點的其他問題。
您可以安全地使用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>
我的輸出沒有任何重復 -
我認為你在n上實現了distinct,它包含了class的所有屬性。 您只需要在必需的列上應用distinct。 喜歡
var empList= DataContext.EmployeeAtds.Select(n => new
{
EmplID= n.EmplID,
EmplName= n.EmplName
}).Distinct().ToList();
要么
var empList= (from n in DataContext.EmployeeAtds select new{ EmplID= n.EmplID,
EmplName= n.EmplName}).Distinct();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.