1

私は ASP.NET を初めて使用し、構文についていくつか質問があります。ride_reservations、車両、および管理オプションに関する情報を保持する SQL DB テーブルがいくつかあります。これは、特定の車両に送信する次に最適な乗車予約を選択する asp.net プログラム機能用です。

最初に最も古い呼び出しでソートされた2つの有効な日付のいずれかにあるdbの乗り物をC#ArrayListに入力することから始めようとしています(そして構文を理解しようとしています)。

前もって感謝します!

**編集: 'TimeOfCall' db 列が 'datetime2(7)' 型で、ValidDate1/2 db 列が 'date' 型であるという別の問題もあります...これらを比較できますか?

いくつかの構文の助けを借りて :below: 私は近づいていますが、次の実行時エラーが発生しています:

シーケンスには要素が含まれていません

説明: 現在の Web 要求の実行中に未処理の例外が発生しました。エラーの詳細とコード内のどこでエラーが発生したかについては、スタック トレースを確認してください。

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

ここに画像の説明を入力

4

1 に答える 1

1

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.

于 2012-01-16T20:31:41.390 に答える