Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have one collection like
strnameCollection
[0]={number:12, name:"abc"}
[1]={number:13, name:"abc, cdf"}
[2]={number:14, name:"tre"}
[3]={number:15, name:"iuj"}


one more collection name like strcollection
[0]={number:12, value:"zzzz"}
[1]={number:13, value:"abc, tyu"}


USE LinQ

do i need to compare both collection and get count for matching records. example "abc" is matching and existing 2 times in strnameCollection.

My Result should be
{number:12, name:"abc"}
{number:13, name:"abc,cdf"}


i want to make using Linq and not foreach loop.
Posted
Updated 12-Jan-15 6:50am
v2
Comments
Zoltán Zörgő 17-Jan-15 6:54am    
Any progress?

Below is your solution.

C#
var strCollection1 = new[] {
               new{number=12, name="abc"},
               new{number=13, name="abc, cdf"},
               new{number=14, name="tre"},
               new{number=15, name="iuj"}
           };

           var strCollection2 = new[] {
               new{number=12, name="abc"},
               new{number=13, name="abc, cdf"},
           };

           var joined = from x1 in strCollection1
                        join x2 in strCollection2
                           on new { x1.name, x1.number } equals
                              new { x2.name, x2.number }
                        select x1;

           foreach (var item in joined)
           {
               Console.WriteLine("{number=" + item.number + ",name=" + item.name + "}");
           }
 
Share this answer
 
What about this:
C#
var common = strCollection1.Intersect(strCollection2);
Console.WriteLine(common.Count());

...using the two collections from Solution 1.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900