怎么不重复名称出现在组合框c#

时间:2012-08-13 01:38:23

标签: c# linq combobox

enter image description here

我遇到了一个问题,我似乎无法使组合框中的名称出现一次而不是多次出现。我的代码中是否有任何导致此问题的内容?任何帮助将不胜感激。

以下是将名称链接到组合框的代码。

private void Create_EmpDetails_Load(object sender, EventArgs e)
    {
        using (satsEntities Setupctx = new satsEntities())
        {
            var viewEmpName = (from viewEN in Setupctx.employees
                               join ufi u in Setupctx.ufis on viewEN.UFISID equals u.UFISID
                               select new { u.EmployeeName , u.UFISID}).Distinct().ToList();

            cbName.DataSource = viewEmpName;
            cbName.DisplayMember = "EmployeeName";
            cbName.ValueMember = "EmployeeName";
            //cbName.ValueMember = "UFISID";

        }
    }

4 个答案:

答案 0 :(得分:2)

其中每一行都有不同的UFISID,因此Distinct()不会删除它们。

听起来你只想向员工展示:

cbName.DataSource = Setupctx.Employees;

答案 1 :(得分:0)

或许,替换

就足够了
select new { u.EmployeeName , u.UFISID}

select new { u.EmployeeName }

答案 2 :(得分:0)

我编辑了我的代码,我设法只显示1个名称,而不是多个记录。

private void Create_EmpDetails_Load(object sender, EventArgs e)
    {
        using (satsEntities Setupctx = new satsEntities())
        {
            var viewEmpName = (from viewEN in Setupctx.employees
                               join ufi u in Setupctx.ufis on viewEN.UFISID equals u.UFISID
                               select new { u.EmployeeName }).Distinct().ToList();

            cbName.DataSource = viewEmpName;
            cbName.DisplayMember = "EmployeeName";
            cbName.ValueMember = "EmployeeName";
            //cbName.ValueMember = "UFISID";

        }
    }

答案 3 :(得分:0)

在combobaxes中,我们为用户显示字符串DisplayAdmber,并为我们显示成员ID(对我们来说可能很重要)作为ValueMember。我们有更多时间使用ID。我的例子:

class Country
{
    public string Name { get; set; }
    public int ID { get; set; }
    public Country( int i,string s) { Name = s; ID = i; }
}

class ComboItem
{

    public string DisplayMember { get; set; }

    public int ValueMember { get; set; }

}


class ComboItemEqualityComparer : IEqualityComparer<ComboItem>
{

    public bool Equals(ComboItem item1, ComboItem item2)
    {
        if (item1.ValueMember == item2.ValueMember && item1.DisplayMember == item2.DisplayMember)
        {
            return true;
        }

        return false;
    }


    public int GetHashCode(ComboItem item)
    {
        string str = item.DisplayMember + item.ValueMember;
        return str.GetHashCode();
    }
}

测试:

 List<Country> countries = new List<Country> {
                                  new Country(1,"UK"), 
                                  new Country(2,"Turkey"), 
                                  new Country(8,"Turkey"),
                                  new Country(5,"Turkey"), 
                                  new Country(2,"Turkey"),
                                  new Country(3,"USA") ,
                                  new Country(3,"USA")};  //.Distinct(new CountryEqualityComparer()).ToList();

        var Data = (from i in countries
                select new ComboItem { ValueMember = i.ID, DisplayMember = i.Name }).Distinct(new ComboItemEqualityComparer()).ToList();



        cbName.DataSource = Data;

        cbName.DisplayMember = "DisplayMember";
        cbName.ValueMember = "ValueMember";

有时我们的数据显示名称相同,但它们的id不是。我们可以将ComboItemEqualityComparer equals方法更改为:

public bool Equals(ComboItem item1, ComboItem item2)
    {
        if (item1.ValueMember == item2.ValueMember )
        {
            return true;
        }

        return false;
    }

享受。

对于这个问题,我们可以:

....
select new ComboItem { ValueMember = u.UFISID, DisplayMember = u.EmployeeName }).Distinct(new yourIEqualityComparer()).ToList();