Asp.net mvc Lambda表达式左连接
我在读这篇文章,但我想不出怎么做。我有这样的表达:Asp.net mvc Lambda表达式左连接,asp.net-mvc,vb.net,asp.net-mvc-3,lambda,Asp.net Mvc,Vb.net,Asp.net Mvc 3,Lambda,我在读这篇文章,但我想不出怎么做。我有这样的表达: Dim myData = db.Tbl_Exercises.Where(Function(x) x.Exercise_Employee_ID) 我的Tbl\u练习模型具有Exercise\u Type\u ID。我想使用该ID从我的Tbl\u exercise\u type模型中获取练习描述的类型,该模型具有exttype\u Desc属性,这是我想在应用程序中使用的属性(代替Tbl\u exercise.exercise\u ID) 我该怎
Dim myData = db.Tbl_Exercises.Where(Function(x) x.Exercise_Employee_ID)
我的Tbl\u练习
模型具有Exercise\u Type\u ID
。我想使用该ID从我的Tbl\u exercise\u type
模型中获取练习描述的类型,该模型具有exttype\u Desc
属性,这是我想在应用程序中使用的属性(代替Tbl\u exercise.exercise\u ID
)
我该怎么做
编辑:
这是我的模型:
Public Class Tbl_Exercise
<Key()> Public Property Exercise_ID() As Integer
Public Property Exercise_Employee_ID() As Integer
Public Property Exercise_Create_Date() As Date
<ForeignKey("Tbl_Exercise_Type")>
Public Property Exercise_Type_ID() As Integer
Public Property Exercise_Duration() As Integer
Public Overridable Property Tbl_Exercise_Type As Tbl_Exercise_Type
End Class
Public Class Tbl_Exercise_Type
<Key()> Public Property ExType_ID() As Integer
Public Property ExType_Desc() As String
Public Property Exercise_Create_Date() As Date
Public Overridable Property Tbl_Exercise() As ICollection(Of Tbl_Exercise)
End Class
Public Class ExerciseDbContext
Inherits DbContext
Public Property Tbl_Exercises As DbSet(Of Tbl_Exercise)
Public Property Tbl_Exercise_Types As DbSet(Of Tbl_Exercise_Type)
End Class
公共课Tbl\U练习
公共属性\u ID()为整数
公共属性\u Employee\u ID()为整数
公共属性练习\u创建\u日期()作为日期
公共属性\u Type\u ID()为整数
公共属性Exercise_Duration()为整数
公共可重写属性Tbl_Exercise_Type作为Tbl_Exercise_Type
末级
公共类Tbl_练习类型
公共属性ExType_ID()为整数
公共属性ExType_Desc()作为字符串
公共属性练习\u创建\u日期()作为日期
公共可重写属性Tbl_Exercise()作为ICollection(Tbl_Exercise的)
末级
公共类上下文
继承DbContext
公共财产Tbl_演习作为DbSet(Tbl_演习的一部分)
公共财产Tbl_演习类型为DbSet(Tbl_演习类型)
末级
谢谢。请参阅下面的翻译
/C#
var OrderItems=Orders.GroupJoin(Items,o=>o.OrderId,i=>i.OrderId,(o,i)=>new{o,i});
'VB.NET
Dim OrderItems=Orders.GroupJoin(项,函数(o)o.OrderId,函数(i)i.OrderId,函数(o,i)用{o,i}新建)
/C#
var Item=OrderItems.SelectMany(oi=>oi.i.DefaultIfEmpty(),(o,i)=>new{o.o,i});
'VB.NET
Dim Item=OrderItems.SelectMany(函数(oi)oi.i.DefaultIfEmpty(),函数(o,i)用{o.o,i}新建)
/C#
变量数量=项目选择(oi=>oi.i.Qty);
'VB.NET
尺寸数量=项目选择(功能(oi)oi.i.Qty)
/C#
var Item=OrderItems.SelectMany(oi=>oi.i.DefaultIfEmpty(),(o,i)=>new{o.o.OrderDate,i.Qty});
'VB.NET
Dim Item=OrderItems.SelectMany(函数(oi)oi.i.DefaultIfEmpty(),函数(o,i)使用{o.o.OrderDate,i.Qty}新建)
完整样本
Class Order
Private _orderId As Integer
Public Property OrderId() As Integer
Get
Return _orderId
End Get
Set(ByVal value As Integer)
_orderId = value
End Set
End Property
End Class
Class Item
Private _orderId As Integer
Public Property OrderId() As Integer
Get
Return _orderId
End Get
Set(ByVal value As Integer)
_orderId = value
End Set
End Property
Private _qty As Integer
Public Property Qty() As Integer
Get
Return _qty
End Get
Set(ByVal value As Integer)
_qty = value
End Set
End Property
End Class
Module Module1
Sub Main()
Dim Orders = New Order() { _
New Order With {.OrderId = 1}, _
New Order With {.OrderId = 2}, _
New Order With {.OrderId = 3} _
}
Dim Items = New Item() { _
New Item With {.OrderId = 1, .Qty = 1}, _
New Item With {.OrderId = 3, .Qty = 1}, _
New Item With {.OrderId = 4, .Qty = 1} _
}
Dim OrderItems = Orders.GroupJoin(Items, Function(o) o.OrderId, Function(i) i.OrderId, Function(o, i) New With {o, i})
Dim Item = OrderItems.SelectMany(Function(oi) oi.i.DefaultIfEmpty(), Function(o, i) New With {o.o, i})
Dim Qty = Item.Select(Function(oi) oi.i.Qty)
' Dim Item = OrderItems.SelectMany(Function(oi) oi.i.DefaultIfEmpty(), Function(o, i) New With {o.o.OrderDate, i.Qty})
End Sub
End Module
那篇文章似乎很清楚。你能更具体地说明你在哪里遇到麻烦吗?我真的不知道怎么做。我不理解我提供的链接中给出的语法。另外,我用的是VB,不是C。。。只要意识到
o=>o.OrderId
相当于Function(o)o.OrderId
和(o,i)=>new{o,i}
到Function(o,i)new With{o,i}
[一分钟内更彻底的翻译…]谢谢,但我想我遗漏了一些东西。我试图创建我的模型(我刚刚添加到问题中),但我得到了一个错误。我不知道如何将你的答案应用到我的场景中。我试图只使用data.Add(New Object(){I.Tbl\u Exercise\u Type.ExType\u Desc,I.Exercise\u Duration})
(无左联接),但我在这一行遇到了一个错误。正如我所说的,您需要更具体一些。您遇到了什么错误?错误只是说,“执行命令定义时发生了错误。有关详细信息,请参阅内部异常。
”我不确定问题出在哪里,但我假设问题出在我的模型中。如果查看InnerException属性,您将获得更多详细信息。只要data.Add(…)
goes。。。你的问题中没有这方面的问题,让我们谈谈