尝试对两个属性进行linq连接,其中两个属性的名称互不相同。
join子句中的表达式之一的类型不正确 加入通话的类型推断失败
这是小提琴https://dotnetfiddle.net/xKg9mB
这是代码
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; }
}
}
答案 0 :(得分:3)
您只需在查询的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
};
这将解决问题。