使用字符串动态创建(LLBLGen)Linq查询 [英] Create (LLBLGen) Linq query dynamicly with strings

查看:192
本文介绍了使用字符串动态创建(LLBLGen)Linq查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要生成编码(设计时)100%未知的LINQ查询。这是因为我们的框架中提供了与任何数据项目100%分离的逻辑。对于数据,我们使用LLBLGen生成的数据访问代码。



通常通过对DLL指定的DLL(不是引用)使用调用,我们可以创建代码来检索数据。但是现在我们需要这样做。我们如何创建一个查询,如:

  var q =来自客户的m.Customer 
选择新
{
customer.Number,
customer.City,
customer.CountryEntity.Name
};

仅来自字符串。我们会有一个叫客户的字符串,所以我们知道我们必须从客户端检索。那么我们将有一个包含我们要检索的字段名的字符串[]。您可以看到这些列可能包括复杂类型(相关字段)。



任何建议,特别是与LLBLGen相结合将是非常好的!



谢谢,
Gab

解决方案

我不知道这是否正是你的寻找,但Scott Gu在他的博客上有一篇关于使用动态LINQ的文章。 http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using -the-linq-dynamic-query-library.aspx



它可能不会做你需要的一切,但可能会让你有所作为。 p>

编辑。我只是看一下Scott Gu的一些示例代码,并发现它可以做你需要的选择部分。示例(这是Scotts代码):

  Dim query = db.Customers.Where(City == @ 0 and Orders Count> = @ 1,London,10)。 _ 
OrderBy(CompanyName)。 _
选择(新(公司名称,电话))

看底部有一个动态选择。



另外,为了解决动态知道在运行时查询哪个对象的问题,您可以这样做:



<$ ($)
Dim i = Myobject.Select(New(customer.Number))
End Sub

然后,您可以在从数据库中读取名称之后做一点切换,像这样:

  Sub PassIt()
Dim name =customer
选择案例名称
案例客户
查询(m.Customer)
结束选择
结束子

希望这有帮助。
注意!会有一个更好的方式做最后一部分(passit方法),但它的清晨想想它。



对不起,答案是在VB我应该已经在C#


中完成了

We need to generate LINQ queries which are 100% unknown during coding (design time). This is because the logic is available in our framework which is 100% separated from any data projects. For data we use LLBLGen generated data access code.

Normally by using invokes on the DLL, which we specify to the framework (not reference) we can create code to retrieve data. But now we need to do this by linq. How could we create a query like:

var q = from customer in m.Customer
        select new
        {                   
            customer.Number,
            customer.City,
            customer.CountryEntity.Name             
        };

from strings only. We would have 1 string called "customer" so we know we have to retrieve from Customer. Then we would have a string[] containing the fieldnames we want to retrieve. As you can see these columns could include complex types (related fields).

Any advice, especially in combination with LLBLGen, would be great!

Thanks, Gab

解决方案

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#

这篇关于使用字符串动态创建(LLBLGen)Linq查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆