Search code examples
sqlsql-serverlinqlinq-to-sqlsql-query-store

Select data between 2 Date in sql and linq


hi i need to select some data between 2 date i can do this easily with sql query like this :
SELECT * FROM tbl_Check WHERE StudentId = 9 AND CheckDate >= 2010 AND CheckDate <= 2017


Note: CheckDate type in Sql Table is: NVARCHAR(12)

now i want use this query in linq i wrote this one but i get error that operator >= cant applied to operand of type string and string.

from val in dc.CheckVs
            where val.StudentId == 9 &&
                  val.CheckDate >= txtDate1.Text &&
                  val.CheckDate <= txtDate2.Text
                  select val;



Solution

  • If you field is NVARCHAR(12) but the value only is YEAR you can convert the columns in INT.

    Try that.

    from val in dc.CheckVs
    where val.StudentId == 9 &&
          Convert.ToInt32(val.CheckDate.Tostring()) >= Convert.ToInt32(txtDate1.Text) &&
          Convert.ToInt32(val.CheckDate.Tostring()) <= Convert.ToInt32(txtDate2.Text)
    select val;
    

    I wait this help you ;)