下拉列表中只有名字

only name in dropdown

我尝试填充下拉值 table 列中的值是

KHI
ISL
LAHR
PARIS
LONDON
BCE-456
BXR-445
BET-567
TYU-112
OPL-456

现在我不想要这些

BXR-445
BET-567
TYU-112
OPL-456 etc etc

我只想要城市名称而不是数字

我试着通过这个来填充

 var list = tea.tblRe.AsEnumerable()
                   .Where(x => !x.Region.Any(char.IsDigit))
                    .ToList();

所以当我尝试上面的查询时,这只显示名称,但像这样重复,例如

   KHI
   ISL
   KHI
   LONDON
   LONDON
   LAHR
   ISL
   ISL
   ISL
   PARIS
   LONDON
   PARIS
   PARIS

更新

好的,我尝试实施 'IEqualityComparer'

class tablregionclass : IEqualityComparer<tblRe>
{
    #region IEqualityComparer<tblRe> Members

    public bool Equals(tblRe x, tblRe y)
    {
        return x.Region.Equals(y.Region);
    }

    public int GetHashCode(tblRe obj)
    {
        return obj.Region.GetHashCode();
    }

    #endregion
}

然后当我像这样class实现这个

 IEqualityComparer<tblRe_New> customComparer = new tablregionclass();
            IEnumerable<tblRe_New> disntregions = collection.Distinct(customComparer); 

这个显示错误

Error   2   The name 'collection' does not exist in the current context 
Error   1   Cannot implicitly convert type 'chart_project.tablregionclass' to 'System.Collections.Generic.IEqualityComparer<chart_project.tblReg_New>'. An explicit conversion exists (are you missing a cast?) 

您可以使用 char.IsDigitDistinct(而不是 GroupBy

var res = tea.tblRegion_Uni.AsEnumerable()
    .Where(x => !x.Region.Any(char.IsDigit))
    .Select(x=> x.Region)
    .Distinct()
    .ToList();

如果您需要绑定到对象列表,则:

var res = tea.tblRegion_Uni.AsEnumerable()
    .Where(x => !x.Region.Any(char.IsDigit))
    .ToList();

我删除了 select 语句和 Distinct!

要使 Distinct 正常工作,您需要实施 IEqualityComparer,或者您可以试试这个:

 var res = tea.tblRegion_Uni.AsEnumerable()
        .Where(x => !x.Region.Any(char.IsDigit))
        .GroupBy(x=>x.Region)
        .Select(x=>new {Region=x.Key, Value=x.Key})
        .ToList();

这里我返回一个带有 2 个属性的匿名对象。

如果你想匹配有数字的,使用正则表达式:

var pattern = \[0-9]\g;
alert(pattern.test("BCE-456"));