[英]Filter parent/child table (one to many association) in linq query based on entities in child table?
[英]Linq : Load parent and child entities
我正在嘗試將SQL Server 2008數據庫中的數據加載到服務器端DTO中以傳輸到客戶端,並且想知道如何加載父級和子級實體的集合。
從模型中可以看到,我正在嘗試加載所有用戶以及在tblUsermmRole表上加入的相關UserRoles。
當我運行Linq查詢時,加載子實體時會崩潰,因為該查詢無法將單個實例加載到集合中。
我可以在單個查詢中加載這些實體,還是應該將用戶加載到集合中,然后迭代構建子AccessRoles?
模型
數據傳輸對象
Public Class User
Public Property ID As Int32
Public Property Username As String
Public Property Password As String
Public Property AccessRoles As IList(Of UserRoles)
End Class
Public Class UserRoles
Public Property Role As ApplicationRole
End Class
Public Class ApplicationRole
Public Property ID As Int32
Public Property Description As String
End Class
資料載入
Dim var = (From usr In ctx.tblUsers.OrderBy(Function(w) w.username)
Join useraccess In ctx.tblUsermmRoles On usr.idUser Equals useraccess.idUser
Join role In ctx.tblUserRoles On role.idRole Equals useraccess.idRole
Select New User With {.ID = usr.idUser,
.Username = usr.username,
.Password = usr.pwd,
.AccessRoles = New ApplicationRole With {.ID = role.idRole,
.Description = role.description}}).ToList
異常消息
{"Unable to cast the type 'Epms.Ui.Models.ApplicationRole' to type 'System.Collections.Generic.IList`1'. LINQ to Entities only supports casting EDM primitive or enumeration types."}
堆棧跟蹤
at System.Data.Objects.ELinq.ExpressionConverter.ValidateAndAdjustCastTypes(TypeUsage toType, TypeUsage fromType, Type toClrType, Type fromClrType)
at System.Data.Objects.ELinq.ExpressionConverter.GetCastTargetType(TypeUsage fromType, Type toClrType, Type fromClrType, Boolean preserveCastForDateTime)
at System.Data.Objects.ELinq.ExpressionConverter.CreateCastExpression(DbExpression source, Type toClrType, Type fromClrType)
at System.Data.Objects.ELinq.ExpressionConverter.ConvertTranslator.TranslateUnary(ExpressionConverter parent, UnaryExpression unary, DbExpression operand)
at System.Data.Objects.ELinq.ExpressionConverter.UnaryTranslator.TypedTranslate(ExpressionConverter parent, UnaryExpression linq)
at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
at System.Data.Objects.ELinq.ExpressionConverter.MemberInitTranslator.TypedTranslate(ExpressionConverter parent, MemberInitExpression linq)
at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.JoinTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod)
at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
at System.Data.Objects.ELinq.ExpressionConverter.Convert()
at System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
at System.Data.Entity.Internal.Linq.InternalQuery`1.GetEnumerator()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Epms.Ui.DataProvider.DataAccess.EntityProvider.GetUsers() in C:\Users\phil.murray\Desktop\Data Provider\Epms.Ui.DataProvider\DataAccess\EntityProvider.vb:line 24
at Epms.Ui.DataProvider.DataProvider.VB$StateMachine_0_GetUsers.MoveNext() in C:\Users\phil.murray\Desktop\Data Provider\Epms.Ui.DataProvider\DataProvider.vb:line 27
最后,我更改為UserRoles類以保存ID int值
Public Class UserRoles
Public Property Role As int32
End Class
然后通過下面的Linq查詢加載數據。
Return (From usr In ctx.tblUsers.Include("tblUsermmRoles")
Select New User With {.ID = usr.idUser,
.Username = usr.username,
.Password = usr.pwd,
.AccessRoles = usr.tblUsermmRoles.Select(Function(r) r.idRole)}).ToList
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.