populate dictionary from the intersection of two list collections RRS feed

  • Question

  • Hi,

    I have a list like List<Keypair<int,List<Items>> X where Items is a class... and another List like List<StudentTasks> Y where

    StudentTasks is a class which contains two variables PersonnelNumber(int),Iseligible(bool).......

    In the above list X contains personnelnumber(int) which is the key for each list member in X...

    In the above list Y contains personnelnumber(int) which is one of the variable in each list member (ie StudentTasks )...

    actually the personnelnumbers in X were the subset of personnelnumbers present in Y which even contains personnal numbers

    other than in X....So as now

    Please let me knoiw how to  fetch personnelnumbers from Y(which were present in X) and the associated Iseligible values from Y...

    and need to create a new Dictionary like   var q=new Dictionary<int,bool>()  where this should be populated with values  from the above as personnelnumbers and iseligible as value....



    Monday, January 30, 2012 5:05 PM


  • Hi shiv455,

    I think you can try to use Join to fetch:

     class Program
            static void Main(string[] args)
                Dictionary<int, string> Numbers;
                Numbers = new Dictionary<int, string>();
                Numbers.Add(0, "zero");
                Numbers.Add(1, "one");
                Numbers.Add(2, "two");
                Numbers.Add(3, "three");
                Numbers.Add(4, "four");
                List<StudentTasks> Tasks = new List<StudentTasks>();
                Tasks.Add(new StudentTasks { PersonnelNumber=0, Iseligible=true, Name="zero" });
                Tasks.Add(new StudentTasks { PersonnelNumber = 1, Iseligible = true, Name = "one" });
                Tasks.Add(new StudentTasks { PersonnelNumber = 2, Iseligible = true, Name = "two" });
                Tasks.Add(new StudentTasks { PersonnelNumber = 3, Iseligible = false, Name = "three" });
                var test = from t in Tasks
                           join n in Numbers on t.PersonnelNumber equals n.Key
                           where t.Iseligible = true
                           select new { t.Name, n.Value };       
        public class StudentTasks
            public int PersonnelNumber { get; set; }
            public bool Iseligible { get; set; }
            public string Name { get; set; }
    Have a nice day.

    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 6, 2012 6:31 AM