Click here to Skip to main content
11,479,162 members (48,957 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# LINQ
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 26-Sep-12 20:42pm
YeeMun619
Edited 26-Sep-12 20:47pm
v3
Comments
Ashraff Ali Wahab at 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?
bjiann87 at 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 at 27-Sep-12 23:36pm
   
then my below solution will work:)

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Please find the solution

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:

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 . . .
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 215
1 OriginalGriff 205
2 F-ES Sitecore 185
3 Frankie-C 120
4 DamithSL 85
0 Sergey Alexandrovich Kryukov 7,890
1 OriginalGriff 7,441
2 Sascha Lefèvre 3,064
3 Maciej Los 2,491
4 Richard Deeming 2,335


Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 27 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100