2014-01-24 68 views
1

我正在從數據庫中選擇名稱列表,根據情況,每個名稱在數據庫中多次存在,沒關係。我現在已經填補了DROPDOWNLIST那些名字,但在下拉列表中的每個名字多次出現:下拉顯示多條記錄

enter image description here

,但我想在DROPDOWN每個名稱1時顯示。我用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; 
       } 
      } 
     } 
    } 
} 

編譯警告恢復1591

回答

1

我認爲你正在實施包含類的所有屬性n個不同的。您只需要在必填列上應用不同的字段。像

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(); 
+0

第二個查詢工作,但爲什麼和如何?我不明白區別 –

+1

因爲在上面的查詢不同工作只有兩個必需的列。 「選擇」消除了負責重複輸入的其餘列。 – nitish

1

您需要確保您的基礎對象覆蓋GetHashCodeEquals才能正常工作。

請參閱這裏瞭解更多信息:

http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx

舉個例子,如果你有一個名爲EmployeeAtd目標需要實現,以瞭解當比較對象是相等的上述兩種方法。

這是一個簡單的例子,但可以考慮的EmployeeAtd對象相同的,如果他們匹配EMPLID的,一個實例的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(); 
} 

這不是最好的做法,但還有很多關於如何做到這一點的其他問題。

What is the best way to implement this composite GetHashCode()

Why is it important to override GetHashCode when Equals method is overridden?

+0

對不起,我沒有得到你,請你可以說這在簡單單詞, –

+0

當然,給我幾分鐘。 : – hutchonoid

+0

謝謝先生,我已經花了將近一天的時間來修復它,但是如果你能修復它,不行,如果你能解決這個問題,我還有其他的工作要做,所以可以節省我的時間 –

1

您可以使用DISTINCT安全,我不知道你的準確面臨的問題。請按照這個例子。說我有一個數據庫表這種方式 - enter image description here

然後我寫下面的存儲過程

USE [Sample] 
GO 

CREATE PROCEDURE GiveNames 
AS 
SELECT DISTINCT(Name) FROM [dbo].[SampleTable] 
GO 

然後,我創建一個MVC項目,並添加EDMX並在下列方式使用控制器的內容 -

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> 

個我的輸出不進行任何複製 -

enter image description here