在投影中使用System.Dynamic.Linq

时间:2015-02-07 17:14:04

标签: vb.net linq

我尝试在投影中使用Dynamic linq,但似乎不起作用。 这是代码:

 Dim mylist = (From t In context.Myobj1s.Where("it.id =5") Select New With { _
         .Parent = t, _
        .child1 = t.ch1s.AsQueryable.Where("vl1 >=7"), _
    .child2 = t.ch2s.Asqueryable.Where("quantity <=4") _
     }).ToList

这会产生错误:

An unhandled exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll

Additional information: LINQ to Entities does not recognize the method 'System.Linq.IQueryable`1[MyEnt.Ch1] Where[Ch1](System.Linq.IQueryable`1[Myent.Ch1], System.String, System.Object[])' method, and this method cannot be translated into a store expression.

我该怎么办? 谢谢!


使用建议,我尝试使用predicateBuilder。这是代码:

Dim pred1 = PredicateBuilder.True(Of Myobj1)()
Dim pred2 = PredicateBuilder.True(Of ch1)()
Dim pred3 = PredicateBuilder.True(Of ch2)()
pred1=pred1.And(Function(t) t.id=5)
pred2=pred2.And(Function(t1) t1.vl1>=7)
pred3=pred3.And(Function(t2) t2.quantity<=4)
Dim mylist = (From t In context.Myobj1s.Where(pred1)Select New With { _
         .Parent = t, _
        .child1 = t.ch1s.Where(pred2), _
    .child2 = t.ch2s.Where(pred3) _
     }).ToList

现在我在设计时遇到了这个错误:

Error   103 Overload resolution failed because no accessible 'Where' can be called with these arguments:
Extension method 'Public Function Where(predicate As System.Func(Of ch1, Integer, Boolean)) As System.Collections.Generic.IEnumerable(Of ch1)' defined in 'System.Linq.Enumerable': Value of type 'System.Linq.Expressions.Expression(Of System.Func(Of Dyqani.FatD_item, Boolean))' cannot be converted to 'System.Func(Of Myent.ch1, Integer, Boolean)'.
Extension method 'Public Function Where(predicate As System.Func(Of ch1, Boolean)) As System.Collections.Generic.IEnumerable(Of ch1)' defined in 'System.Linq.Enumerable': Value of type 'System.Linq.Expressions.Expression(Of System.Func(Of MyEnt.ch1, Boolean))' cannot be converted to 'System.Func(Of MyEnt.ch1, Boolean)'.
Extension method 'Public Function Where(predicate As String, ParamArray values() As Object) As System.Collections.Generic.IEnumerable(Of ch1)' defined in 'System.Linq.Dynamic.DynamicQueryable': Value of type 'System.Linq.Expressions.Expression(Of System.Func(Of myent.ch1, Boolean))' cannot be converted to 'String'

有什么问题?

0 个答案:

没有答案