0

在此处输入图像描述

我遇到了一个问题,我似乎无法让组合框中的名称出现一次而不是多个。我的代码中有什么会导致这个问题吗?任何帮助将不胜感激。

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

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

4 回答 4

2

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

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

cbName.DataSource = Setupctx.Employees;
于 2012-08-13T01:46:14.717 回答
0

可能你换个就够了

select new { u.EmployeeName , u.UFISID}

select new { u.EmployeeName }
于 2012-08-13T01:56:20.587 回答
0

我对此进行了编辑,并设法仅显示了一个名称而不是多个记录。

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";

        }
    }
于 2012-08-13T02:03:55.383 回答
0

在组合框中,我们将字符串显示为用户的 DisplayMember 和成员的 id(可能对我们很重要)作为我们的 ValueMember。我们有更多时间使用 ids 。我的例子:

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();
于 2012-08-13T03:35:35.830 回答