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 6012391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:23:52+00:00 2026-05-23T02:23:52+00:00

I have a simple table that basically has a NVARCHAR Name column and a

  • 0

I have a simple table that basically has a NVARCHAR Name column and a DateTime Timestamp column. I want to write a C# function that takes a collection of Names and DateTimes, and return everything from my table where the Names match and the DateTimes in the collection are older than the DateTimes in the table.

The simple way my non-SQL brain came up with is to create a SELECT statement to get all of the matching Names (“SELECT Name,Timestamp From MyTable WHERE Name IN (…)”) and then iterate through the returned collection comparing the DateTimes.

There has to be a better way to do this with SQL or Linq. How can I more efficiently do this?

UPDATE:
Thanks for the helpful feedback! Sounds like running the query analyzer on the various approaches listed here will be the way to go. Here’s another one to throw out there, if any are curious:

List<Item> items = ... // this the collection
var db = new ItemDataContext(); // From .Net EF

  string[] results = (from item in items
                      from dbItem in db.Items
                      where item.Name.Equals(dbItem.Name,StringComparison.InvariantCultureIgnoreCase)
                            && item.Timestamp < dbItem.Timestamp
                      select dbItem.Token).ToArray();
  • 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-23T02:23:53+00:00Added an answer on May 23, 2026 at 2:23 am

    Using a large IN list is not neccessarily bad, but you may get into problems if you have thousand of elements in the list since the numnber of parameters may cause problems.

    But basically the other approach would be to join on some (temporary) table. Using that strategy would however require you to insert the data into the table, so unless you can reuse the query many times the work involved with inserting the filter data would largely exceed the work in returning the results.

    The best advice here as anywhere else would be to look at the query analyzer for results though.

    An example of the second approach using a temporary table is shown below. Here you may get the benefit of using indexes in the JOIN, but this should be contrasted to the cost of the inserts for each filter value and the complexity in the code utilizing the filter function. (A simple IN clause is autogenerated by LINQ, while the temp table approach would require quite some plumming in the code.)

       CREATE TABLE vals (v nvarchar(255), inserted DATETIME DEFAULT(getdate()))
    
       SELECT * FROM vals 
         WHERE v IN ('a', 'aa', 'aaa')
    
       DECLARE @filters TABLE (v varchar(256))
    
       INSERT INTO @filters (v) VALUES ('a')
       INSERT INTO @filters (v) VALUES ('aa')
       INSERT INTO @filters (v) VALUES ('aaa')
    
       SELECT * FROM vals INNER JOIN @filters f
         ON f.v = vals.v
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple table (lets call it Table1) that has a NVARCHAR field
I have a simple cakephp app with table articles that has a cat_id column
I have a submission table that is very simple: userId, submissionGuid I want to
I have a table that has, among its primary keys, a VARCHAR(16) column that
i have a very simple table with only one row that has a uiswitch
Say I have a simple table that contains username, firstname, lastname. How do I
I use SQL Server 2005. I have a simple logging table that my web
If I have a simple table where the data is such that the rows
I have two simple tables in my database. A card table that contains Id,
I'm trying to have a simple html table, that highlights a row as a

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.