Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 林克加入。。。“进入”不会返回已连接的对象_C#_Linq_Join_Linq To Sql - Fatal编程技术网

C# 林克加入。。。“进入”不会返回已连接的对象

C# 林克加入。。。“进入”不会返回已连接的对象,c#,linq,join,linq-to-sql,C#,Linq,Join,Linq To Sql,我不熟悉SQL和LINQ。我尝试了使用join…into语法连接两个列表的简单代码,但结果并不是我所期望的 public static void Main(string[] args) { IEnumerable<KeyValuePair<char,int>> list1 = new []{ new KeyValuePair<char,int>( 'a', 1) , new KeyValuePair<char,i

我不熟悉SQL和LINQ。我尝试了使用join…into语法连接两个列表的简单代码,但结果并不是我所期望的

public static void Main(string[] args)
{
    IEnumerable<KeyValuePair<char,int>> list1 = new []{ 
        new KeyValuePair<char,int>( 'a', 1) ,
        new KeyValuePair<char,int>( 'b', 2) , 
        new KeyValuePair<char,int>( 'c', 3)  };
    IEnumerable<KeyValuePair<char, int>> list2 =  new[]{
        new KeyValuePair<char,int>( 'b', 10) ,
        new KeyValuePair<char,int>( 'c', 20) ,
        new KeyValuePair<char,int>( 'd', 30)  };

    var joinQuery = from x in list1
                    join y in list2
                    on x.Key equals y.Key into joinTable
                    from t in joinTable
                    select new { element = t };

    foreach (var el in joinQuery)
        Console.WriteLine(el);
}
我所期望的是joinTable包含连接的记录,类似于:

{element = {[b, 2], [b, 10]}}
{element = {[c, 3], [c, 20]}}
你能解释一下。。。实际上,在joinTable中,为什么我可以在上次选择中使用x,而不能使用y:


如果我理解正确的话,您基本上是在尝试从列表1和列表2中获取所有信息,它们在一个键上匹配。如果是,您可以这样做:

var joinQuery = from x in list1
                join y in list2
                on x.Key equals y.Key
                select new
                {
                    first = x,
                    second = y
                };

您无需将其添加到任意表中,只需从联接结果中进行选择。

如果我理解正确,您基本上是在尝试从列表1和列表2中获取所有信息,它们在一个键上匹配。如果是,您可以这样做:

var joinQuery = from x in list1
                join y in list2
                on x.Key equals y.Key
                select new
                {
                    first = x,
                    second = y
                };
您不需要将其添加到任意表中,只需从联接结果中选择即可。

根据联接。。。转换为语法将转换为GroupJoin,而不是预期的Join

public static void Main(string[] args)
{
    IEnumerable<KeyValuePair<char,int>> list1 = new []{ 
        new KeyValuePair<char,int>( 'a', 1) ,
        new KeyValuePair<char,int>( 'b', 2) , 
        new KeyValuePair<char,int>( 'c', 3)  };
    IEnumerable<KeyValuePair<char, int>> list2 =  new[]{
        new KeyValuePair<char,int>( 'b', 10) ,
        new KeyValuePair<char,int>( 'c', 20) ,
        new KeyValuePair<char,int>( 'd', 30)  };

    var joinQuery = from x in list1
                    join y in list2
                    on x.Key equals y.Key into joinTable
                    from t in joinTable
                    select new { element = t };

    foreach (var el in joinQuery)
        Console.WriteLine(el);
}
但你真正想要的是真正的加入,就像这样:

var joinQuery = from x in list1
                join y in list2
                on x.Key equals y.Key                     
                select new { x, y };
在查询中,您无法访问y,因为联接到的语法不同。你不需要另一个来自。。。joinTable,但必须直接访问joinTable:

var joinQuery = from x in list1
                join y in list2
                on x.Key equals y.Key into joinTable
                select new {joinTable.x, matches = joinTable.y.ToList()};
但这将导致y拥有列表2中的所有匹配元素。这就是导致每个匹配元素一行的联接和将匹配项分组在一起的GroupJoin之间的区别。

根据联接。。。转换为语法将转换为GroupJoin,而不是预期的Join

public static void Main(string[] args)
{
    IEnumerable<KeyValuePair<char,int>> list1 = new []{ 
        new KeyValuePair<char,int>( 'a', 1) ,
        new KeyValuePair<char,int>( 'b', 2) , 
        new KeyValuePair<char,int>( 'c', 3)  };
    IEnumerable<KeyValuePair<char, int>> list2 =  new[]{
        new KeyValuePair<char,int>( 'b', 10) ,
        new KeyValuePair<char,int>( 'c', 20) ,
        new KeyValuePair<char,int>( 'd', 30)  };

    var joinQuery = from x in list1
                    join y in list2
                    on x.Key equals y.Key into joinTable
                    from t in joinTable
                    select new { element = t };

    foreach (var el in joinQuery)
        Console.WriteLine(el);
}
但你真正想要的是真正的加入,就像这样:

var joinQuery = from x in list1
                join y in list2
                on x.Key equals y.Key                     
                select new { x, y };
在查询中,您无法访问y,因为联接到的语法不同。你不需要另一个来自。。。joinTable,但必须直接访问joinTable:

var joinQuery = from x in list1
                join y in list2
                on x.Key equals y.Key into joinTable
                select new {joinTable.x, matches = joinTable.y.ToList()};

但这将导致y拥有列表2中的所有匹配元素。这就是导致每个匹配元素一行的Join和分组匹配的GroupJoin之间的区别。

我知道我可以做到这一点。我问了两个问题,为什么它不能与into语法一起使用,以及为什么如果我将y变量与into语法一起使用,则y变量不可见select@Petar-我的错。我以为你在问为什么你不能得到你想要的结果,是因为与“进入”有关吗?我知道我可以做到这一点。我问了两个问题,为什么它不能与into语法一起使用,以及为什么如果我将y变量与into语法一起使用,则y变量不可见select@Petar-我的错。我以为你在问为什么你不能得到你想要的结果,这是因为与“进入”有关吗