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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:03:24+00:00 2026-06-13T03:03:24+00:00

I’m having some trouble with a t-sql select query in C#. The query is

  • 0

I’m having some trouble with a t-sql select query in C#. The query is as follows:

@"SELECT * FROM 
 (SELECT *, ROW_NUMBER() OVER (ORDER BY id) as row_number
 FROM [mytable]) t0
 WHERE row_number BETWEEN @skip and @take
 AND
 uploadDate IN (@years) 
 AND NOT photoId IN (@shownPhotos) 
 ORDER BY NEWID()";

cmd.Parameters.Add("@skip", SqlDbType.Int).Value = skip;
cmd.Parameters.Add("@take", SqlDbType.Int).Value = take;
cmd.Parameters.Add("@shownPhotos", SqlDbType.VarChar).Value = shownPhotos;
cmd.Parameters.Add("@years", SqlDbType.VarChar).Value = years;

The method takes four parameters: skip count, take count, shownPhotos (commaseparated string which contains photo ids), years (comma separated string which contains the years to show the photos from)

So basically I want it to return photos from my database which matches any of the years from the commaseparated string and which are not already shown.

The problem (I think) is that my parameters are strings, so they are intepreted as ‘1234,567,890’ and ‘2011,2012’. When running the query in the SQL server management tool without the ‘s it works fine.

Anyone got a workaround for this? 🙂

Thanks a lot in advance!

Updated code:

// Create datatables
            DataTable yearsTable = new DataTable();
            yearsTable.Columns.Add("YearID", typeof(string));
            yearsTable.Columns.Add("Year", typeof(string));

            foreach(string year in years.Split(','))
                yearsTable.Rows.Add(new object[] { year, year });

            using (var conn = new SqlConnection(GlobalSettings.DbDSN))
            {
                conn.Open();
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText =
                        @"SELECT * FROM 
                          (SELECT *, ROW_NUMBER() OVER (ORDER BY id) as row_number
                          FROM [mytable]) t0
                          WHERE row_number BETWEEN @skip and @take
                          AND
                          uploadedDate IN (@years)                               
                          ORDER BY NEWID()";

                    SqlParameter skipParam = cmd.Parameters.AddWithValue("@skip", skip);
                    skipParam.SqlDbType = SqlDbType.Int;

                    SqlParameter takeParam = cmd.Parameters.AddWithValue("@take", take);
                    takeParam.SqlDbType = SqlDbType.Int;

                    SqlParameter yearsParam = cmd.Parameters.AddWithValue("@years", yearsTable);
                    yearsParam.SqlDbType = SqlDbType.Structured;
                    yearsParam.TypeName = "dbo.MyTableType";


                    var reader = cmd.ExecuteReader();                        
                }
  • 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-06-13T03:03:25+00:00Added an answer on June 13, 2026 at 3:03 am

    Indeed, your IN are being interpreted as “is in this set of 1 value”, i.e. what you have is identical to:

    AND uploadDate = @years
    AND NOT photoId = @shownPhotos
    

    evaluated as regular string equality on the strings '1234,567,890' and '2011,2012'. At the TSQL level, a common trick is to write a “split” UDF that separates a single varchar into multiple row values. Many libraries have tools for this too; for example, in “dapper” we adding a syntax trick to pre-expand them, i.e.

    int[] years = {2011,2012};
    int[] shownPhotos = {1234,567,890};
    int skip = 0, take = 10;
    var rows = connection.Query<RowType>(
      @"SELECT * FROM 
       (SELECT *, ROW_NUMBER() OVER (ORDER BY id) as row_number
       FROM [mytable]) t0
       WHERE row_number BETWEEN @skip and @take
       AND uploadDate IN @years
       AND NOT photoId IN @shownPhotos
       ORDER BY NEWID()", new {skip,take,years,shownPhotos}).ToList();
    

    Note the lack of brackets next to IN, which we interpret as “figure this out yourself” to the library, which then does exactly what you want. It will actually be evaluated as:

       AND uploadDate IN (@years0,@years1)
       AND NOT photoId IN (@shownPhotos0,@shownPhotos1,@shownPhotos2)
    

    where @years0 has the value 2011, @years1 has the value 2012, etc.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
I'm trying to select an H1 element which is the second-child in its group

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.