Convert T-SQL to Linq

Posted on 2011-05-10
Last Modified: 2012-05-11
Hello, could someone convert this to Linq2Sql please?

FROM            Customers
WHERE  (FirstName LIKE @FirstName OR @FirstName IS NULL OR @FirstName = '')
  AND  (LastName LIKE @LastName OR @Lastname IS NULL or @LastName = '')
  AND  (PostCode LIKE @PostCode OR @PostCode IS NULL or @PostCode = '')
  AND  (EmailAddress LIKE @EmailAddress OR @EmailAddress IS NULL OR @EmailAddress = '')
  AND  (ContactTel LIKE @ContactTel OR @ContactTel IS NULL OR @ContactTel = '')

Question by:badpilot
    LVL 18

    Expert Comment

    C# like this:
    var result = from customer in  Customers
    where customer.FirstName.StartWith(youpatameter...) AND .............  
    select customer
    LVL 13

    Assisted Solution

    hi ,

    try this way

    var cust  = from objCustomer in customer 
    where (objCustomer.FirstName.Contains("Mark") && objCustomer.FirstName != null)
    && (objCustomer.LastName .Contains("Anthony") && objCustomer.LastName  != null)
    && (objCustomer.PostCode .Contains("12345k") && objCustomer.PostCode  != null)
    select objCustomer;

    Open in new window

    LVL 21

    Accepted Solution

    var cust  = from objCustomer in customer
    where (objCustomer.FirstName.Contains("Mark") || objCustomer.FirstName == null || objCustomer.FirstName == string.empty)
    && (objCustomer.LastName .Contains("Anthony") || objCustomer.LastName  == null || objCustomer.LastName  == string.empty)
    && (objCustomer.PostCode .Contains("12345k") || objCustomer.PostCode  == null || objCustomer.PostCode  == string.empty)
    select objCustomer;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
    If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
    Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
    Viewers will learn how the fundamental information of how to create a table.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now