问题描述
我有2个清单(A和B).我想通过使用(*)通配符加入两个列表来获得预期的结果(如下所示).有人有主意吗?是LinQ的事,但花1天仍然无法获得任何答案.
总之,我想做的是表格Data_List_A第一行A_Field1 => a *,当A_Field2与和B_Field2之间联接时,"_ Field2"中的a *将能够与所有以"a"开头的B_Field2联接.
有谁知道如何通过使用列表连接功能做到这一点?谢谢.
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 =>新的{A_Field2},
b =>新{B_Field2}
(a,b)=> new {A_Field1,A_Field2,A_Field3,A_Field4,B_Field1,B_Field2,B_Field3});
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});
推荐答案
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 . . .
这篇关于c#LinQ使用通配符(*)连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!