我是否可以通过代码更改linq中的位置? [英] Hod Do I change a where in a linq by code?

查看:53
本文介绍了我是否可以通过代码更改linq中的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要一些能让我在代码中更改where子句的东西。

例如,在下面的代码中,

如果IDtipo<> 0,我可以直接做第二个linq。

有什么帮助?

谢谢

Hi guys,
I need something that allow me to change a where clause in code.
For example, in the follow code,
if IDtipo <>0, i can do the second linq directly.
Any helps?
Thank you

Shared Function getTipologiaCanone(ByVal IDTipo As Integer) As List(Of ConfiguratoreCanoniFissi.CanoniTipo)
    Dim lista_canoni As List(Of ConfiguratoreCanoniFissi.CanoniTipo)
    Using dbContext As New P2000ProdwareEntities
        lista_canoni = (From c In dbContext.CanoniTipoes
                        Select c).ToList

        If IDTipo <> 0 Then
            lista_canoni = (From c In lista_canoni
                          Where c.id_tipo = IDTipo
                          Select c).ToList
        End If

    End Using
    Return lista_canoni
End Function

推荐答案

LINQ的一个好处是你可以建立你的分阶段查询。如果您在结束前避免调用 ToList ,那么您的查询将被转换为高效的数据库查询,并且只会将相关记录加载到内存中。

One of the benefits of LINQ is that you can build your query up in stages. If you avoid calling ToList until the end, then your query will be translated into an efficient database query, and only the relevant records will be loaded into memory.
Shared Function getTipologiaCanone(ByVal IDTipo As Integer) As List(Of ConfiguratoreCanoniFissi.CanoniTipo)
    Using dbContext As New P2000ProdwareEntities
        Dim lista_canoni As IQueryable(Of ConfiguratoreCanoniFissi.CanoniTipo)
        lista_canoni = dbContext.CanoniTipoes
        
        If IDTipo <> 0 Then
            lista_canoni = From c In lista_canoni
                Where c.id_tipo = IDTipo
                Select c
        End If
        
        Return lista_canoni.ToList()
    End Using
End Function


一种方法是将where子句的更改部分隔离到您自己的方法中您可以控制项目是否包含在结果中。在这种方法中,LINQ查询结构保持不变。



请考虑以下示例:



数据类

One way is that you isolate the changing part of the where clause to your own method where you control if the item is included in the result or not. In this kind of approach the LINQ query structure stays the same.

Consider the following example:

Data class
Public Class MyItem
   Public Property BoolValue As Boolean
   Public Property TextValue As String
End Class



决定是否包含该项目的扩展方法


Extension method to decide if the item is included or not

Module ItemExtension
   <System.Runtime.CompilerServices.Extension()>
   Public Function IsValid(ByVal myItem As MyItem, textStart As String) As Boolean
      IsValid = myItem.TextValue.StartsWith(textStart) And myItem.BoolValue = False
   End Function
End Module



和测试运行


And a test run

Dim myList As List(Of MyItem) = New List(Of MyItem)
Dim myItem As MyItem
Dim resultList As System.Collections.Generic.IEnumerable(Of MyItem)

myItem = New MyItem
With myItem
   .BoolValue = False
   .TextValue = "First"
End With
myList.Add(myItem)
myItem = New MyItem
With myItem
   .BoolValue = True
   .TextValue = "Second"
End With
myList.Add(myItem)

resultList = From i In myList
             Where i.IsValid("Fi") = True
             Select i



所以基本上你可以将你喜欢的任何参数传递给扩展方法并在其中应用更改逻辑。


So basically you could pass whatever parameters you like to the extension method and apply the changing logic inside of it.


这篇关于我是否可以通过代码更改linq中的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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