locked
use between in linq

    Question

  •  

    dears,

     

    i can do taht in sql

     

    select * from table where date between 9/9/2007 and 1/1/2008

     

    how i can use between keyword and and in linq

     

    thanks,

    Thursday, February 14, 2008 12:09 PM

Answers

  • Hi,

     

    The obvious answer would be something like...

     

    Code Snippet

    var query = from t in table

    where t.date >= new DateTime(2007,9,9) && t.date < new DateTime(2008,1,1) select t;

     

     

     

    Thursday, February 14, 2008 1:35 PM

All replies

  • Hi,

     

    The obvious answer would be something like...

     

    Code Snippet

    var query = from t in table

    where t.date >= new DateTime(2007,9,9) && t.date < new DateTime(2008,1,1) select t;

     

     

     

    Thursday, February 14, 2008 1:35 PM
  •  

    really it ia a very good blog and it worked fine for me

     

    thanks

    sai

    Tuesday, September 30, 2008 9:38 AM
  • var query = from post in Context.Table where input_date >= post.start_date && input_date <= post.end_date select post
    Monday, December 01, 2008 8:01 AM
  • Hi,

    This works for DateTime, how about if you want to try the same thing for type string.

    For eg. I have a column 'ItemCode' in table with type varchar (it is primary key).

    I can make the query in database like

    Select * from tb1 where ItemCode >='A001' and ItemCode<='A004'

     

    When i try this something like this

    IQueryable<ItemMaster> oitmQuery = from ot in oitm

    where ot.ItemCode >=itemCodeFrom && ot.ItemCode<=itemCodeTo

     

     I get following error

    Operator '>=' cannot be applied to operands of type 'string' and 'string'

     

     

    If anybody came has solution, please share.

     

    Thanks a lot

    Rakesh
    Tuesday, January 13, 2009 10:06 PM
  • I figure out the solution for this:

     

     

    ObjectQuery<ItemMaster> iMaster =

    objMRPEnt.ItemMasterSet

    .Where("it.ItemCode>= @itemCodeFrom and it.ItemCode<=@itemCodeTo"

    , new ObjectParameter("itemCodeFrom", itemCodeFrom), new ObjectParameter("ItemcodeTo", itemCodeTo));

     

     

    Thanks

    Rakesh

    Tuesday, January 13, 2009 11:24 PM
  • Hello, may someone could help me on one problem in query LINQ to SQL Visual Basic.

     

    My database  has three tables,

    • Students(StudentID, StudentName),
    • Class(ClassID, ClassDate),
    • Attendance(StudentID, ClassID) which Attendance tables in the junction table between Students and Class.

    For example:

    Students

    StudentsID             StudentName

    1001                     Smith

    1002                     John

    1003                     Bob

     

    Class

    ClassID                  ClassDate

    1                       1/1/2009

    2                       1/3/2009

    3                       1/7/2009

     

    Attendance

    StudentID             ClassID

    1001                   1

    1002                   1

    1003                   1

    1001                   2

    1003                   2

    1001                   3

    1002                   3

    1003                   3

     

    I have try the query Visual Basic LINQ to SQL below to list all the student and the count number of attendance in a range of date.

     

    1 Dim db = New AttendanceControllerDataContext
    2
    3 Dim result = From std In db.Students _
    4 Select std.StudentID, _
    5 std.StudentName, _
    6 Attendance = std.Attendances.Count()
    7
    8 GridView1.DataSource = result
    9 GridView1.DataBind()

     

    In line 6, Attendance I want to count according to the range of date in the Class Table, but I don't know how to do this. May someone help me please....

    Wednesday, April 08, 2009 1:24 AM
  • Obvious to those already proficient in LINQ! ;-)

    Very helpful to those of us just learning LINQ--thanks.

    Friday, August 20, 2010 7:43 PM
  • what is this ObjectQuery<ItemMaster> and this is not coming in my code

    pls send  some sample code where i wanted to range between them and show in datagrid

    i m already working this code but not working

    var s=from p in tablelist

    where p.cat_no>=

    txtHeatStart.Text.ToString() && p

    .cast_no <= txtHeatEnd.Text.ToString ()

     

     

     datagridname.itemsource=s;

    pls send me ............

    Friday, August 05, 2011 10:04 AM