LINQ Contains Case Insensitive

Goal: Search for content in the database via LINQ to ENTITIES or LINQ to SQL that is case-insensitive (case-agnostic).

Example:

var con = BusinessContext.Create();
var owners = con.Shipments.Where(s =>                                       
                                          s.Dispatches.All(x => x.DriverLBCompany.Contains("(exempt)")
                                          ).Select(x => x);

This will fetch all dispatches where the DriverLBCompany contains the word “(exempt)”. You do not have to be concerned about the case as SQL Server is case-agnostic when it performs filtering. The sql being generated by this statement is:

FROM [dbo].[Shipment] AS [Extent1]
WHERE  NOT EXISTS (SELECT 
    ….

    FROM [dbo].[Dispatch] AS [Extent2]
    WHERE (( NOT ([Extent2].[DriverLBCompany]
    LIKE '%(exempt)%'))
    OR (CASE WHEN ([Extent2].[DriverLBCompany] LIKE '%(exempt)%')
    THEN cast(1 as bit) WHEN ( NOT ([Extent2].[DriverLBCompany] LIKE '%(exempt)%'))
    THEN cast(0 as bit) END IS NULL))
)

Print | posted @ Thursday, October 17, 2013 7:38 AM

Comments on this entry:

Gravatar # Search for content in the database via LINQ to ENTITIES or LINQ to SQL that is case-insensitive (case-agnostic).
by Lenders Allies at 10/17/2013 4:50 PM

Hey hi thanks for given this knowledgeable information. I like it.......


http://www.lendersallies.com
Post A Comment
Title:
Name:
Email:
Comment:
Verification: