Saturday, 22 March 2014

linq join two lists

Join is self defined at the joining of two or more data together; Here I am instructed join of list object in C#;

@linq join two lists
var result = from Item1 in listItem1
             join Item2 in listItem2
             on Item1.Id equals Item2.Id // Join values
             select new { Item1, Item2 };

@get the result in form of concrete list,  needed to call .ToList()
Get the result in the form of list
  var list3 = (from Item1 in list1
             join Item2 in list2
             on Item1.Id equals Item2.Id // join on some property
             select new { Item1, Item2 }).ToList()

@Linq Union operator in C#
Linq Union operator will give the combined unique result of to list contains,
static void Main(string[] args)
        {
            List<string> name1 = new List< string >() { "a", "b", "c"};
            List< string > name2 = new List< string >() {"c", "d" };
            List<string> uniqueName = name1.Union(name2).ToList();
            foreach(string st in uniqueName)
            {
                Console.WriteLine(st);
            }
        }
It will give the result as
a
b
c
d

No comments:

Post a Comment

Others

You will like to read it