Create (LLBLGen) Linq query dynamicly with strings

喜你入骨 提交于 2019-12-04 17:14:27

I'm not sure if this is exactly what your looking for but Scott Gu has a post on his blog about using dynamic LINQ.http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

It may not do everything you need but it may get you some of the way.

EDIT. I was just having a look at some sample code that Scott Gu had, and found that it can do the select part that you need. Example(This is Scotts code):

Dim query = db.Customers.Where("City == @0 and Orders.Count >= @1", "London", 10). _
                OrderBy("CompanyName"). _
                Select("New(CompanyName as Name, Phone)")

As you can see the bottom bit has has a dynamic select.

Also to solve the problem of dynamically knowing which object to query at runtime, you could something like this:

 Sub query(Of T)(ByVal Myobject As IQueryable(Of T))
    Dim i = Myobject.Select("New(customer.Number)")
 End Sub

Then you could just do a little switch after you read the names from the database, like so:

Sub PassIt()
    Dim name = "customer"
    Select Case name
        Case "customer"
            query(m.Customer)
    End Select
End Sub

Hope this helps. Note! There would be a better way of doing the last part(passit method) but its to early in the morning to think of it.

Sorry the answer is in VB I should have done it in C#

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!