Search code examples
c#asp.netsqllinq-to-sqlentitydatasource

LINQ : ASP.NET EntityDataSource Beginner's Syntax


I am new to ASP.NET and have some questions about syntax... I have several SQL DB Tables that hold information about ride_reservations, vehicles, and admin options. This is for an asp.net program feature that will select the next best ride reservation to send to a certain vehicle.

I am attempting to begin (and figure out syntax) by populating a C# ArrayList with the rides in the db that are on one of two valid dates, sorted by oldest call first.

Thanks in advance!

**Edit: also another issue is that the 'TimeOfCall' db column is a 'datetime2(7)' type and the ValidDate1/2 db colums are type 'date'...can I compare these?

With some syntax help :below: I am closer but am getting the following runtime error:

Sequence contains no elements

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Sequence contains no elements

Source Error: 


Line 27:         protected void getAllRides()
Line 28:         {
Line 29:             using (RamRideOpsEntities myEntities = new RamRideOpsEntities())
Line 30:             {
Line 31:                 var adminOptions = (from a in myEntities.AdminOptions

Source File: D:\DOCUMENTS\RamRide\RamRideOps_PL\RamRideOps\RamRideOps\Ops\DispatchCar.aspx.cs    Line: 29 

Stack Trace: 


[InvalidOperationException: Sequence contains no elements]
   System.Linq.Enumerable.First(IEnumerable`1 source) +336
   System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__0(IEnumerable`1 sequence) +41
   System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle(IEnumerable`1 query, Expression queryRoot) +59
   System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute(Expression expression) +150
   System.Linq.Queryable.First(IQueryable`1 source) +265
   RamRideOps.DispatchCar.getNextRide(Int32 carNum) in D:\DOCUMENTS\RamRide\RamRideOps_PL\RamRideOps\RamRideOps\Ops\DispatchCar.aspx.cs:29
   RamRideOps.DispatchCar.Page_Load(Object sender, EventArgs e) in D:\DOCUMENTS\RamRide\RamRideOps_PL\RamRideOps\RamRideOps\Ops\DispatchCar.aspx.cs:24
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272

enter image description here


Solution

  • validDate1 and validDate2 will both be Queryable AdminOptions

    you probably want this

    var adminOptions = (from a in myEntities.AdminOptions
                                 select new { a.ValidDate1, a.ValidDate2 }).First();
    

    Assumes Table with fields ValidDate1 & ValidDate2 (not a propertyBag style with Keys and Values)

    now you can get your rides like this

    var rides = (from r in myEntities.Rides
                               where (r.TimeOfCall == adminOptions.ValidDate1 || 
                                      r.TimeOfCall == adminOptions.ValidDate2)
                               orderby TimeOfCall descending
                               select r).ToList();
    

    The ToList() will avoid the need to do a loop at the end to build an array.

    That should pretty much fix your syntax.

    EDIT

    I missed putting the r in TimeOfCall in the orderby line

    var rides = (from r in myEntities.Rides
                               where (r.TimeOfCall == adminOptions.ValidDate1 || 
                                      r.TimeOfCall == adminOptions.ValidDate2)
                               orderby r.TimeOfCall descending
                               select r).ToList();
    

    I think you really need to look at doing a few of the tutorials around the place, or buy a good book on the subject. An afternoon of reading might save you a bunch of frustration.