Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7640495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:41:06+00:00 2026-05-31T08:41:06+00:00

I’ve solved my problem, but because I have tried and failed to solve it

  • 0

I’ve solved my problem, but because I have tried and failed to solve it before, and this time it took me a good deal of effort, I wanted to post the question, and if nobody has a better answer, my solution so I don’t forget how to do this in the future, and to help anybody else facing a similar challenge. My challenge is this:

I have a function used to filter a list of options to return only those that are lot traced, and, when applicable, also only show items matching a specified pattern:

  Private Shared Function FilterResultsLot(ByVal source As IQueryable(Of Item), _
     ByVal itemCode As String) As IQueryable(Of Item)
     If HasFilter(itemCode, True) Then
        Return From row In source Where row.ItemDetail.IsLotTraced AndAlso _
               row.ItemCode.ToLower() Like itemCode.ToLower()
     Else
        Return From row In source Where row.ItemDetail.IsLotTraced
     End If
  End Function

Another requirement came up and I wanted to be able to generalize the lot traced criteria into a general filter, so I created this function:

  Private Shared Function FilterResults(source As IQueryable(Of Item), _
     itemCode As String, filter As Func(Of Item, Boolean)) As IQueryable(Of Item)
     Dim predicate As Func(Of Item, Boolean)
     If HasFilter(itemCode, True) Then
        predicate = Function(row) row.ItemCode.ToLower() Like itemCode.ToLower() AndAlso filter.Invoke(row)
     Else
        predicate = filter
     End If
     Return source.Where(predicate).AsQueryable()
  End Function

And that function works through LINQ-to-SQL, but it loses significant optimization. Whereas the first function would an execute SQL query ending with:

FROM [dbo].[OITM] AS [t0]
LEFT OUTER JOIN [dbo].[FSE_ItemDetail] AS [t1] ON [t1].[ItemCode] = [t0].[ItemCode]
WHERE ([t1].[IsLotTraced] = 1) AND (LOWER([t0].[ItemCode]) LIKE @p0 ESCAPE ''~'')

The second would execute multiple SQL queries with these endings (and potentially many more):

FROM [dbo].[OITM] AS [t0]

FROM [dbo].[FSE_ItemDetail] AS [t0]
WHERE [t0].[ItemCode] = @p0',N'@p0 nvarchar(4000)',@p0=N'BF-BIKE'

FROM [dbo].[FSE_ItemDetail] AS [t0]
WHERE [t0].[ItemCode] = @p0',N'@p0 nvarchar(4000)',@p0=N'BF-BIKE-ITEM'

So the question is how to combine the LINQ-to-SQL delegate expressions into one without calling Invoke or AsQueryable, or without losing the optimal execution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:41:07+00:00Added an answer on May 31, 2026 at 8:41 am

    Here’s my current solution. Please comment or provide better solutions if you have any:

      Private Shared Function FilterResults(source As IQueryable(Of Item), itemCode As String, filter As Expressions.Expression(Of Func(Of Item, Boolean))) As IQueryable(Of Item)
         If HasFilter(itemCode, True) Then
            Return Queryable.Where(Queryable.Where(source, Function(row) row.ItemCode.ToLower() Like itemCode.ToLower()), filter)
         Else
            Return Queryable.Where(source, filter)
         End If
      End Function
    

    Edit:

    After working with it a bit more, I prefer a simplification/alternative to this syntax:

    Return Queryable.Where(Queryable.Where(source, _
       Function(row) row.ItemCode.ToLower() Like itemCode.ToLower()), filter)
    

    My preference is:

    Return Queryable.Where(From row in source _
       Where row.ItemCode.ToUpper() Like itemCode.ToUpper(), filter)
    

    (I also suggest ToUpper instead of ToLower to force case-insensitive match on case-sensitive server because I’ve heard UPPER is better optimized for that.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.