0

I have 2 lists groupoptions and dataIndicator

class groupoptions{
  ...
  GroupName string
}

class dataIndicator{
  ...
 HeaderID int
 IndicatorDescription
}

currently my groupname possible values are 4 or 5 or 11

first i want to get all dataindicators where headerid is equal to GroupName, then replace those GroupNames with dataindicator's IndicatorDescription

What would be the Linq syntax for this?

UPDATE

using join

var newList = from first in dataIndicator
                      join second in groupedoptions
                      on first.HeaderID.ToString() equals second.GroupName
                      select new { first,second };

What next? PROBLEM: i want to do it inside a constructor that is being created inside a Linq Select

var list = xyz.Select(x => new groupoptions(){
   GroupName = x.Key.ToString();
 })
Samra
  • 1,815
  • 4
  • 35
  • 71

3 Answers3

1

this little for loop gets what you are looking to do done in just a few lines. No need to use linq if all you're looking to do is be efficient.

class ConsoleApplication1
{
    public static void Main()
    {

        List<groupOptions> g = new List<groupOptions>();
        List<dataIndicator> d = new List<dataIndicator>();

        for (int i = 0; i < 4; i++)
        {
            g.Add(new groupOptions() { groupName = i.ToString() });
            d.Add(new dataIndicator() { headerID = i, indicatorDescription = "id:" + i});
            Console.Write(g[i].groupName + ":");
            Console.WriteLine(d[i].headerID);
        }

        Console.WriteLine("enter to change");
        Console.ReadLine();

this is the pertinent part:

        for(int i = 0; i < g.Count(); i++)
        {
            if (g[i].groupName == d[i].headerID.ToString())
                g[i].groupName = d[i].indicatorDescription;
        }

the rest just is just to make sure it worked.

        for(int i = 0; i < d.Count(); i++)
        {
            Console.WriteLine(g[i].groupName);
        }

        Console.ReadLine();
    }
}

public class groupOptions
{

    public string groupName { get; set; }

}


class dataIndicator
{
    public int headerID { get; set; }
    public string indicatorDescription { get; set; }
}
Alex Durbin
  • 196
  • 1
  • 7
0

my problem was i wanted to do it inside a constructor that was being created inside a Linq Select...i did the following instead of join

var list = xyz.Select(x => new groupoptions()
        {      
          GroupName = dataIndicator.Where(d => d.IndicatorID.ToString().Equals(x.Key.ToString())).First().IndicatorDescription
            }

P.S. First is taken because there will always be one record that matches..Also i was doing a mistake of comparing with headerid when i should have compared to indicatorid property.

Samra
  • 1,815
  • 4
  • 35
  • 71
0

Here's a non-linq example:

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

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            var groupOptions = new List<GroupOption>();
            groupOptions.Add(new GroupOption { GroupName = 4.ToString() });
            groupOptions.Add(new GroupOption { GroupName = 5.ToString() });
            groupOptions.Add(new GroupOption { GroupName = 11.ToString() });

            var dataIndicators = new List<DataIndicator>();
            dataIndicators.Add(new DataIndicator { HeaderID = 4, IndicatorDescription = "four" });
            dataIndicators.Add(new DataIndicator { HeaderID = 99, IndicatorDescription = "ninetynine" });
            dataIndicators.Add(new DataIndicator { HeaderID = 100, IndicatorDescription = "onehundred" });

            Console.WriteLine("Before:");
            PrintGroupOptions(groupOptions);

            foreach (var dataIndicator in dataIndicators)
            {
                foreach (var groupOption in groupOptions)
                {
                    if (dataIndicator.HeaderID.ToString() == groupOption.GroupName)
                    {
                        groupOption.GroupName = dataIndicator.IndicatorDescription;
                    }
                }
            }

            Console.WriteLine("After:");
            PrintGroupOptions(groupOptions);
        }

        static void PrintGroupOptions(List<GroupOption> groupOptions)
        {
            groupOptions.ToList().ForEach(g => Console.WriteLine(g));
        }
    }

    class GroupOption
    {
        public string GroupName { get; set; }
        public override string ToString()
        {
            return GroupName;
        }
    }

    class DataIndicator
    {
        public int HeaderID { get; set; }
        public string IndicatorDescription { get; set; }
    }
}
mariocatch
  • 8,305
  • 8
  • 50
  • 71