可能真的很容易,但我就是看不到它......
我在 LINQ 中复制 MS Access 查询。我首先用 C# 编写它来测试它,因为我更喜欢 C#,然后我将它翻译成 VB.Net 语法。据我所知,这两个查询应该是相同的,但是 C# 查询返回正确的结果,而 VB.NET 一个返回零结果。
任何人都可以看到差异可能在哪里?
C# 查询:
var table1 = dc.MainTable.Where(o => o.Year == 423).ToList().Select(o => new
{
Key_ID = o.Key_ID.Value,
CropID = o.CropID.Value,
GroupID = o.GroupID.Value,
Surface1 = o.Surface1.Value,
Surface2 = o.Surface2.Value
});
var table2 = dc.OtherTable.Where(o => o.Year == 423).ToList().Select(o => new
{
Key_ID = o.Key_ID.Value,
CropID = int.Parse(o.SAKU_CD),
GroupID = int.Parse(o.SAN_DAN_NO),
Surface1 = Convert.ToDouble(o.KEIHAN_MEN.Value),
Surface2 = Convert.ToDouble(o.SAKU_MEN.Value)
});
var output = table1.Join(table2, t1 => new
{
t1.Key_ID,
t1.CropID,
t1.GroupID,
t1.Surface1,
t1.Surface2
},
t2 => new
{
t2.Key_ID,
t2.CropID,
t2.GroupID,
t2.Surface1,
t2.Surface2
}, (t1, t2) => new OutputDataType()
{
Key_ID = t1.Key_ID,
Year = 423
}).ToList();
VB.NET 查询:
Dim table1 = MainTable.Where(Function(o) o.Year.Value = 423).ToList().Select(Function(o) New With
{
.Key_ID = o.Key_ID.Value,
.CropID = o.CropID.Value,
.GroupID = o.GroupID.Value,
.Surface1 = o.Surface1.Value,
.Surface2 = o.Surface2.Value
}).ToList()
Dim table2 = OtherTable.Where(Function(o) o.Year.Value = 423).ToList().Select(Function(o) New With
{
.Key_ID = o.Key_ID.Value,
.CropID = Convert.ToInt32(o.SAKU_CD),
.GroupID = Convert.ToInt32(o.SAN_DAN_NO),
.Surface1 = Convert.ToDouble(o.KEIHAN_MEN.Value),
.Surface2 = Convert.ToDouble(o.SAKU_MEN.Value)
}).ToList()
Dim output = table1.Join(table2, Function(t1) New With
{
t1.Key_ID,
t1.CropID,
t1.GroupID,
t1.Surface1,
t1.Surface2
}, Function(t2) New With
{
t2.Key_ID,
t2.CropID,
t2.GroupID,
t2.Surface1,
t2.Surface2
}, Function(t1, t2) New OutputDataType With {.Key_ID = t1.Key_ID, .Year = 423}).ToList()
在 C# 和 VB.Nettable1
中table2
都是一样的,所以它一定是Join
失败的。
编辑
我只是将Join
VB.Net 中的更改为查询语法,如下所示:
Dim output = From t1 In MainTable
Join t2 In OtherTable
On t1.Key_ID Equals t2.Key_ID And t1.GroupID Equals t2.GroupID And t1.CropID Equals t2.CropID And t1.Surface1 Equals t2.Surface1 And t1.Surface2 Equals t2.Surface2
Select New OutputDataTypeData With {.Key_ID = t1.Key_ID, .Year = 423}
这给出了正确的结果。但我真的不明白这与扩展方法Join
语法有何不同?