Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have 2 List (A and B). I want to get the expected results (shown below) by joining two list with the (*) wildcard Character. anyone have an idea?? is something abt LinQ but spend 1 day still cant get any ans.

In summary what i wanna to do is Form Data_List_A 1st row A_Field1 => a*,when join between A_Field2 with and B_Field2, the a* in "_Field2" will able to join with all B_Field2 which start with "a".

Does anyone know how to make it by using List join function?? thanks.

Data_List_A
A_Field1, A_Field2, A_Field3, A_Field4
1, a*, 123A, aaa123
2, bbb, 123A, aaa123
3, ccc, 123A, aaa123

Data_List_B
B_Field1, B_Field2, B_Field3
1, aaa, 123B
2, aab, 123B
3, aac, 123B
4, bbb, 123B
5, ccc, 123c

Result_List
A_Field1, A_Field2, A_Field3, A_Field4, B_Field1, B_Field2, B_Field3
1, a*, 123A, aaa123, 1, aaa, 123B
1, a*, 123A, aaa123, 2, aab, 123B
1, a*, 123A, aaa123, 3, aac, 123B
2, bbb, 123B, aaa123, 4, bbb, 123B
3, ccc, 123C, aaa123, 5, ccc, 123B

var Result_List = Data_List_A.Join(Data_List_B,
a => new { A_Field2},
b => new {B_Field2}
(a, b) => new { A_Field1, A_Field2, A_Field3, A_Field4, B_Field1, B_Field2, B_Field3});
Posted
Updated 26-Sep-12 19:47pm
v3
Comments
Ashraff Ali Wahab 27-Sep-12 12:54pm    
If it is b* it should join with all the rows with the field starting with b?
If it is ab* it should join with all the rows with the field starting with ab?
Tina Ng 27-Sep-12 22:30pm    
yup. if is ab* it will join with all the rows with the field starting with ab....
Ashraff Ali Wahab 27-Sep-12 23:36pm    
then my below solution will work:)

1 solution

Please find the solution

C#
class A
   {
       public string aFieldOne { get; set; }
       public string aFieldTwo { get; set; }
       public string aFieldThree { get; set; }
   }

   class B
   {
       public string bFieldOne { get; set; }
       public string bFieldTwo { get; set; }
       public string bFieldThree { get; set; }
   }



public void joinUsingLinq()
       {
           List<A> listA = new List<A>();
           listA.Add(new A { aFieldOne = "a*", aFieldTwo = "123a", aFieldThree = "aaa123" });
           listA.Add(new A { aFieldOne = "bbb", aFieldTwo = "123a", aFieldThree = "aaa123" });
           listA.Add(new A { aFieldOne = "ccc", aFieldTwo = "123a", aFieldThree = "aaa123" });

           List<B> listB = new List<B>();
           listB.Add(new B { bFieldOne = "aaa", bFieldTwo = "b123a", bFieldThree = "abaa123" });
           listB.Add(new B { bFieldOne = "aab", bFieldTwo = "b123a", bFieldThree = "abaa123" });
           listB.Add(new B { bFieldOne = "aac", bFieldTwo = "b123a", bFieldThree = "abaa123" });
           listB.Add(new B { bFieldOne = "bbb", bFieldTwo = "b123-a", bFieldThree = "abaa123" });
           listB.Add(new B { bFieldOne = "ccc", bFieldTwo = "b123-a", bFieldThree = "abaa123" });

           var a = from dataA in listA
                   from dataB in listB
                   let matcher = new System.Text.RegularExpressions.Regex(WildcardToRegex(dataA.aFieldOne))
                   let matches = matcher.Matches(dataB.bFieldOne)
                   where matches.Count>0
                   select new
                   {
                       dataA.aFieldOne,
                       dataA.aFieldTwo,
                       dataA.aFieldThree,
                       dataB.bFieldOne,
                       dataB.bFieldTwo,
                       dataB.bFieldThree
                   };

           foreach (var x in a)
               Console.WriteLine("aFieldOne: {0}, aFieldTwo:{1}, aFieldThree:{2} bFieldOne: {3}, bFieldTwo:{4}, bFieldThree:{5}",
               x.aFieldOne, x.aFieldTwo, x.aFieldThree, x.bFieldOne, x.bFieldTwo, x.bFieldThree);



       }

       public static string WildcardToRegex(string pattern)
       {
           return "^" + System.Text.RegularExpressions.Regex.Escape(pattern).
           Replace("\\*", ".*").
           Replace("\\?", ".") + "$";
       }


Output:

SQL
aFieldOne: a*, aFieldTwo:123a, aFieldThree:aaa123 bFieldOne: aaa, bFieldTwo:b123a, bFieldThree:abaa123
aFieldOne: a*, aFieldTwo:123a, aFieldThree:aaa123 bFieldOne: aab, bFieldTwo:b123a, bFieldThree:abaa123
aFieldOne: a*, aFieldTwo:123a, aFieldThree:aaa123 bFieldOne: aac, bFieldTwo:b123a, bFieldThree:abaa123
aFieldOne: bbb, aFieldTwo:123a, aFieldThree:aaa123 bFieldOne: bbb, bFieldTwo:b123-a, bFieldThree:abaa123
aFieldOne: ccc, aFieldTwo:123a, aFieldThree:aaa123 bFieldOne: ccc, bFieldTwo:b123-a, bFieldThree:abaa123
Press any key to continue . . .
 
Share this answer
 
v2

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