Conditionally binding dropdownlist in asp.net

171 Views Asked by At

I have a string in this particular format

string LogInIDs = 124,345, 876 | 765,322, 98 | 565,99

All the numbers in the string are Unique LogIn ID. Using the Pipe symbol, the grouping is done of id's. Now, Suppose the LogInID is 345, then i need to bind other numbers (in this case 124 & 876) which are in the group in a dropdown. The below function is what i have made to retrieve the other numbers.

Can anybody come up with any better idea or suggestion

public static List<string> RetrieveAffCodes(string LogInIDs , string LogInID)
{
    List<string> _finale = new List<string>();

    string[] sep1 = new string[1];
    sep1[0] = "|";

    int count = LogInIDs.Count(x => x == '|');
    string[] groups = new string[count + 1];
    groups = LogInIDs.Split(sep1, StringSplitOptions.RemoveEmptyEntries);

    foreach (string g in groups)
    {
        string p = g;
        string[] sep2 = new string[1];
        sep2[0] = ",";
        int counter = p.Count(x => x == ',');
        string[] final_list = new string[counter + 1];
        final_list = p.Split(sep2, StringSplitOptions.RemoveEmptyEntries);

        foreach (string _f in final_list)
        {
            if (_f.Trim() == LogInID)
            {
          _finale =  AddLogInIDs(final_list, final_list.Count());
            }
        }
    }
    return _finale;
}

private static List<string> AddLogInIDs(string[] final_list, int p)
{
    List<string> _finale = new List<string>();
    foreach (var item in final_list)
    {
        _finale.Add(item.Trim());
    }
    return _finale;
}

Any suggestions will be embraced.

Thanks for your time and patience.

Note: The string will be expanding up to 200 groups

2

There are 2 best solutions below

0
levent On

try this..

    public static List<string> RetrieveAffCodes(string logInIDs, string logInID)
    {
        return logInIDs
        .Split("|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
        .Where(a => a.Split(',').Any(c => c.Trim().Equals(logInID)))
        .Select(a => a.Split(',').ToList()).FirstOrDefault();
    }
2
hardkoded On

Would this work for you?

using System;
using System.Collections.Generic;           
using System.Linq;

public class Program
{
    public static void Main()
    {
        string LogInIDs = "124,345,876|765,322,98 |565,99";

        Console.WriteLine(string.Join("\n", RetrieveAffCodes(LogInIDs, "322")));
        Console.WriteLine(string.Join("\n", RetrieveAffCodes(LogInIDs, "565")));
    }

    public static IEnumerable<string> RetrieveAffCodes(string logInIDs , string logInID)
    {
        //We split the list
        var list = logInIDs.Split('|');
        //We look for an item with the logInID, if found (?) we split using ',' and then we remove the item
        var match = list
            .Select(i => i.Split(',').Select(item => item.Trim()))
            .FirstOrDefault(i => i.Contains(logInID))?
            .Where(i => i != logInID);

        if(match != null)
        {
            return match;
        }

        return new List<string>();

    }
}

Source code: https://dotnetfiddle.net/M2FyNk