C# 具有不同名称的多个属性上的c linq连接
尝试在两个属性上执行linq连接,其中属性具有不同的名称 join子句中某个表达式的类型不正确 对加入的调用的类型推断失败 这是小提琴 这是密码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&
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
};
这将解决问题