Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.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连接_C#_.net_Linq - Fatal编程技术网

C# 具有不同名称的多个属性上的c linq连接

C# 具有不同名称的多个属性上的c linq连接,c#,.net,linq,C#,.net,Linq,尝试在两个属性上执行linq连接,其中属性具有不同的名称 join子句中某个表达式的类型不正确 对加入的调用的类型推断失败 这是小提琴 这是密码 using System; using System.Collections.Generic; using System.Linq; namespace linq2 { class Program { static void Main(string[] args) { List&

尝试在两个属性上执行linq连接,其中属性具有不同的名称

join子句中某个表达式的类型不正确 对加入的调用的类型推断失败

这是小提琴

这是密码

using System;
using System.Collections.Generic;
using System.Linq;

namespace linq2
{
    class Program
    {
        static void Main(string[] args)
        {
            List<MyClass1> list1 = new List<MyClass1>() { new MyClass1() { thing1 = "hello", thing2 = "world" } };
            List<MyClass2> list2 = new List<MyClass2>() { new MyClass2() { thing3 = "hello", thing4 = "world" } };

            var bothLists = from l1 in list1
                            join l2 in list2 on new { l1.thing1, l1.thing2 } equals new { l2.thing3, l2.thing4 }
                            select new
                            {
                                thing1 = l1.thing1,
                                thing2 = l1.thing2
                            };

            Console.WriteLine("Hello World!");
        }
    }

    public class MyClass1
    {
        public string thing1 { get; set; }
        public string thing2 { get; set; }
    }

    public class MyClass2
    {
        public string thing3 { get; set; }
        public string thing4 { get; set; }
    }
}

您只需重命名查询的join子句中的属性即可:

from l1 in list1
join l2 in list2 
on new { a = l1.thing1, b = l1.thing2 } equals new { a= l2.thing3, b = l2.thing4 }
select new
{
      thing1 = l1.thing1,
      thing2 = l1.thing2
 };
这将解决问题