使用字符串动态创建 (LLBLGen) Linq 查询

发布于 2024-07-08 14:56:07 字数 624 浏览 7 评论 0原文

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

通常,通过使用 DLL 上的调用(我们向框架指定(而不是引用)),我们可以创建代码来检索数据。 但现在我们需要通过 linq 来完成此操作。 我们如何创建这样的查询:

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

仅来自字符串。 我们将有 1 个名为“customer”的字符串,因此我们知道必须从 Customer 检索。 然后我们将有一个包含我们要检索的字段名的 string[]。 正如您所看到的,这些列可能包含复杂类型(相关字段)。

任何建议,尤其是与 LLBLGen 结合使用,都会很棒!

谢谢, 瞎扯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌辣 2024-07-15 14:56:07

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

它可能无法完成您需要的所有操作,但它可能会为您提供一些帮助。

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

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

正如您所看到的,底部位有一个动态选择。

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

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

然后您可以在从数据库中读取名称后进行一些切换,如下所示:

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

希望这会有所帮助。
笔记! 会有更好的方法来完成最后一部分(passit 方法),但要一大早才能想到它。

抱歉,答案是在 VB 中,我应该在 C# 中完成

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#

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文