code

VB를 사용하여 'where' 절을 추가하려면 어떻게 해야 합니까?NET과 LINQ?

starcafe 2023. 5. 8. 22:21
반응형

VB를 사용하여 'where' 절을 추가하려면 어떻게 해야 합니까?NET과 LINQ?

저는 VB가 처음입니다.NET과 제가 생각하기에 단순해야 할 것으로 여기서 약간의 문제를 겪고 있습니다.

간단히 말하자면, 검색할 "이름"이 있는 문서 테이블이 있다고 가정해 보겠습니다(실제로 다른 테이블, 조인 등이 있습니다).▁a를 사용하여 쿼리를 할 수 .where전달된 문자열 값을 기반으로 하는 절입니다.

예 - 사용자는 "ABC", "ABC DEF", "ABC DEF GHI"를 전달할 수 있습니다.

마지막 쿼리는 다음과 같습니다(구문이 정확하지 않습니다, 알고 있습니다).

Select * from Documents Where Name Like %ABC% AND Name Like %DEF% AND Name like %GHI%

그래서 저는 이런 것을 할 수 있다고 생각했습니다.

Dim query = From document In _context.Documents

<< loop based on number of strings passed in >>
query = query.Where( ... what goes here?? )

어떤 이유에서인지, 뇌사인지 뭔지, 저는 VB에서 이것을 어떻게 작동시킬 수 있는지 모르겠습니다.NET, 아니면 내가 제대로 하고 있는지.

VB(나는 C# 개발자입니다)에서 다음과 같이 작업할 수 있다고 생각합니다.

query = query.Where(Function(s) s = "ABC")

몇 가지 는 LINQ - 샘플 쿼리를 참조하십시오.

여기서 까다로운 부분은 알 수 없는 쿼리 매개 변수라고 생각합니다.여기에서 기본 LINQ IQueryable(Of T)을 사용하여 도움을 받을 수 있습니다.

다음과 같이 하면 될 것 같습니다(컴파일되지 않고, 여기 메모장 코드만 있음).

Public Function GetDocuments(criteria as String)
    Dim splitCriteria = SplitTheCriteria(criteria)

    dim query = from document in _context.Documents

    For Each item in splitCriteria
        Dim localItem = item
        query = AddCriteriaToQuery(query, localItem)
    Next

    dim matchingDocuments = query.ToList()
End Function

Private Function AddCriteriaToQuery(query as IQueryable(Of Document), criteria as string) as IQueryable(Of Document)
     return query.Where(Function(doc) doc.Name = criteria)
End Function

LINQ는 쿼리를 지연 실행하므로 루프의 쿼리에 where 절을 추가한 다음 호출할 수 있습니다.쿼리를 실행할 끝의 ToList()입니다.

LINQ to SQL에서는 를 사용하여 WHERE 절을 쿼리에 추가할 수 있습니다.질문에서 언급한 대로 쿼리 개체의 메서드입니다.LIKE 연산자를 사용하려면 를 사용해 보십시오.Where 메서드에 대한 호출의 람다 식에서 쿼리하는 개체의 메서드를 포함합니다.

다음은 콘솔 응용 프로그램의 단순화된 예입니다.그것이 당신을 올바른 방향으로 이끌기를 바랍니다.

Public Class Doc

    Private _docName As String
    Public Property DocName() As String
        Get
            Return _docName
        End Get
        Set(ByVal value As String)
            _docName = value
        End Set
    End Property

    Public Sub New(ByVal newDocName As String)
        _docName = newDocName
    End Sub
End Class

Sub Main()
    Dim Documents As New List(Of Doc)
    Documents.Add(New Doc("ABC"))
    Documents.Add(New Doc("DEF"))
    Documents.Add(New Doc("GHI"))
    Documents.Add(New Doc("ABC DEF"))
    Documents.Add(New Doc("DEF GHI"))
    Documents.Add(New Doc("GHI LMN"))

    Dim qry = From docs In Documents

    qry = qry.Where(Function(d) d.DocName.Contains("GHI"))

    Dim qryResults As List(Of Doc) = qry.ToList()

    For Each d As Doc In qryResults
        Console.WriteLine(d.DocName)
    Next

End Sub

참고하십시오.의 람다 식에 ("GHI") 호출을 포함합니다.어디서 방법.DocName 속성을 노출하고 추가로 노출하는 식의 매개 변수 "d"를 참조하고 있습니다.메서드를 포함합니다.이렇게 하면 예상되는 LIKE 쿼리가 생성됩니다.

이 메서드는 가법적입니다. 즉, 에 대한 호출입니다.쿼리의 WHERE 절에 추가 LIKE 연산자를 추가하기 위해 메소드를 루프로 묶을 수 있습니다.

Dim query = From document In _context.Documents where document.name = 'xpto' select document 

또는

Dim query = From document In _context.Documents where document.name.contains('xpto') select document 

이 작업을 반복적으로 수행하면 다음과 같은 작업을 수행할 수 있습니다.

.Where(Function(i as mytype) i.myfiltervar = WhatIWantToSelect)

언급URL : https://stackoverflow.com/questions/782566/how-do-i-append-a-where-clause-using-vb-net-and-linq

반응형