比较一串数组并构建不匹配的列表

时间:2011-06-05 01:19:40

标签: asp.net

UPDATE1:

这一切看起来都不错,但是我一直在寻找的大问题,到目前为止我还没有找到。 - 在代码中查看我的评论。

var custNames = LoadCustNames();         var custFirstInitials =(来自custNames中的cn                                  select cn.Name.Substring(0,1).ToLower()                                 ).Distinct();

    foreach (var item in custFirstInitials)
    {

        bool has = list.Contains(item);  
        //the above line of code return true because its checking whether `list.Contains or item.Contails` this will always return true but what i want is something sequence checking the first loop checks for A (found? true) and second loop checks for B (false) and third loop checks for C (false) .......so that i can generate ahref based on whether its return true or false.

        if (has)
        {
            //generate href
        }
        else
        {
            //disable href
        }
    }

我更新了我的问题,似乎问题不明确......

所以你去.... 我正在尝试将字符串[] myArray与字符串进行比较,如果有任何不匹配的内容,则将其存储在字符串中。

举个例子:

我有一个以A,S&开头的custName列表Z并与myArray进行比较

string[] myArray = { "a", "b", "c", "d", "e", "f", "g", .... "z"}; 

public class CustNames
{
    public string Name { get; set; } 
    public CustNames() { }
    public CustNames(string name) 
    { 
       Name = name;
    }
}

public List<CustNames> LoadCustNames()
{
    //connect to db and load the data.
    List<CustNames> names = new List<CustNames>
        {
            new CustNames("Alan"),
            new CustNames("Scoot"),
            new CustNames("Shark"),
            new CustNames("Alpha"),
            new CustNames("Zebra") 
        };
    return names;
}

//page_load event....
   //...check for postback....
    for (int i = 0; i < 5; i++)
    {
        string name = LoadCustNames()[i].Name.ToString(); 
        ListItem(name);
    }

List<char> HeaderOf = new List<char>();
protected void ListItem(string cust)
{
    // need a way to compare with myArray... 

    char CheckMe = cust.Substring(0, 1).ToUpper()[0];
    if (!HeaderOf.Contains(CheckMe))
    {
        HeaderOf.Add(CheckMe);
    }
}

2 个答案:

答案 0 :(得分:0)

我通常做的是创建一个自定义属性来存储事件处理程序调用。因此,当我想要禁用它时,我删除事件处理程序并将其放在自定义属性中。这将删除手形光标,然后您可以在需要重新启用它时添加代码。否则它仍然会有一个误导用户的手形光标。

答案 1 :(得分:0)

禁用它但仍保留为href(点击它不会做任何事情但仍然看起来像活动链接)

b.InnerHtml = "<a onclick='return false;' class='WhateverClassForStyle' href=#" + nextRole.ToUpper().ToString() + ">" + nextRole.ToUpper().ToString() + "</a>";

不要显示为href

b.InnerHtml = "<span class='WhateverClassForStyle'>" + nextRole.ToUpper().ToString() + "</span>";

使用css类WhateverClassForStyle以您想要的方式设置样式。

我想您正在尝试获取客户名称首字母的列表。

var custNames = LoadCustNames();
var custFirstInitials = (from cn in custNames
                            select cn.Name.Substring(0, 1)
                        ).Distinct();

custFirstInitials现在是IEnumerable<string>,您可以对其进行循环或检查是否应该创建href。

foreach (var letter in myArray)
{
    if (custFirstInitials.Contains(letter.ToUpper()))
    {
        // Enabled Href
    }
    else
    {
        // Disabled Href
    }
}