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

  • SEARCH
  • Home
  • 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 964795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:53:19+00:00 2026-05-16T01:53:19+00:00

Can I improve this LINQ query var filter = from Dep in deptlist where

  • 0

Can I improve this LINQ query

var filter = from Dep in deptlist
where (Dep.DepNm.StartsWith(txt1.Text.ToLower()) 
  || Dep.DepNm.StartsWith(txt1.Text.ToUpper())
  ||Dep.DepNm.Contains(txt1.Text)) 
select Dep;
  • 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-16T01:53:20+00:00Added an answer on May 16, 2026 at 1:53 am

    Currently, you do a .Text, .Text.ToUpper() and .Text.ToLower() of the fixed value per item; (the ToUpper() etc being relatively expensive); you can lift this out:

    string text = txt1.Text, upper = text.ToUpper(), lower = text.ToLower();
    var filter = from Dep in deptlist
                 where Dep.DepNm.StartsWith(lower) || Dep.DepNm.StartsWith(upper)
                       || Dep.DepNm.Contains(text)) 
                 select Dep;
    

    I’m assuming here that .DepNm is trivially cheap. If this is actually an expensive property to query, you can use let to minimise the calls:

    var filter = from Dep in deptlist
                 let name = Dep.DepNm
                 where name.StartsWith(lower) || name.StartsWith(upper)
                       || name.Contains(text)) 
                 select Dep;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I improve this username/password checking? [AcceptVerbs(HttpVerbs.Post)] public ActionResult Login(FormCollection collection) { var
Can this MS-Access query be improved? i.e. the SELECT TOP 1 col FROM Table2
How can I improve this code? What has made this long winded is the
I have read that you can do it, but would this really improve performance
are there any improvements where i can improve this code? Maybe there are some
I have this LINQ query which returns the indexes of all the items in
How can improve this sort method to meet the following conditions: exact matches returned
Given an EmployeeId, how can I construct a Linq to Sql query to find
From MSDN : By eliminating unnecessary casts, implicit conversions can improve source code readability.
Would like to know how I can improve this code to enable no page

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.