0

我有两个包含以下数据的列表(列表):

清单一

James
Jack
Laura
Harry

清单二

Jeffery
Peters
Smith

是否可以使用 Enumerable Join 来执行以下操作:

James Jeffery
James Peters
James Smith
Jack Jeffery
Jack Peters
Jack Smith
Laura Jeffery
Laura Peters
Laura Smith
Harry Jeffery
Harry Peters
Harry Smith
Harry Jeffery

这是什么类型的Join?

4

5 回答 5

2

这是一个交叉连接:

List<string> textLines1 = new List<string>() {
    "Kames", "Jack", "Laura", "Harry"
};
List<string> textLines2 = new List<string>() {
    "Jeffery", "Peters", "Smith"
};

var query = from t1 in textLines1
            from t2 in textLines2
            select string.Format("{0} {1}", t1, t2);

var results = query.ToList();

返回您所期望的。

也可以使用基于方法的查询来完成:

var query2 = textLines1.SelectMany(t1 => textLines2, (t1, t2) => string.Format("{0} {1}", t1, t2));

var results2 = query2.ToList();
于 2013-03-12T11:06:56.843 回答
1

在 Linq 中,它将是:

List<string> TabOne = new List<string>() { "Kames", "Jack", "Laura", "Harry" };
List<string> TabTwo = new List<string>() { "Jeffery", "Peters", "Smith" }; 

var combo = from p in TabOne
        from c in TabTwo
        select new
        {
            Name = p,
            SurName = c
        };

它被称为交叉连接。

于 2013-03-12T11:06:37.780 回答
0
var l1 = new []{ "James", "Jack", "Laura", "Harry" };
var l2 = new []{ "Jeffery", "Peters", "Smith" };

var result = from i1 in l1
             from i2 in l2
             select i1 + " " + i2;
于 2013-03-12T11:07:31.680 回答
0

这也被称为Cartesian ProductEric 有一篇关于它的博客文章

于 2013-03-12T11:10:16.933 回答
0

您可以尝试这种方式:

var result = list1.SelectMany(i1 => list2.Select(i2 => i1 + " " + i2));

例子 :

class Program
{
    public static void Main()
    {
        string[] list1 = new[] { "James", "Jack", "Laura", "Harry" };
        string[] list2 = new[] { "Jeffery", "Peters", "Smith" };
        var result = list1.SelectMany(i1 => list2.Select(i2 => i1 + " " + i2));

        foreach (string s in result)
        {
            Console.WriteLine(s);
        }
    }
}

应用输出:

James Jeffery
James Peters
James Smith
Jack Jeffery
Jack Peters
Jack Smith
Laura Jeffery
Laura Peters
Laura Smith
Harry Jeffery
Harry Peters
Harry Smith
于 2013-03-12T11:11:41.767 回答