PDA

View Full Version : مبتدی: ارور هنگام انتخاب سط آخر جدول بوسیله دستورات EF



1000net
یک شنبه 10 اسفند 1393, 14:59 عصر
سلام دوستان
یک جدول دارم میخام آخرین سطر را انتخاب و cid رو بریزم داخل تکست باکس اما متاسفانه ارور نال میده ؟چکار کنم؟
کد:

var query = (from F in mydb.customers select F).Last
.txtcid.text= query.cid
اینم ارور:
System.NotSupportedException was unhandled
Message=LINQ to Entities does not recognize the method 'hesabdan.customer Last[customer](System.Linq.IQueryable`1[hesabdan.customer])' method, and this method cannot be translated into a store expression.
Source=System.Data.Entity
StackTrace:
at System.Data.Objects.ELinq.ExpressionConverter.Meth odCallTranslator.DefaultTranslator.Translate(Expre ssionConverter parent, MethodCallExpression call)
at System.Data.Objects.ELinq.ExpressionConverter.Meth odCallTranslator.TypedTranslate(ExpressionConverte r parent, MethodCallExpression linq)
at System.Data.Objects.ELinq.ExpressionConverter.Type dTranslator`1.Translate(ExpressionConverter parent, Expression linq)
at System.Data.Objects.ELinq.ExpressionConverter.Tran slateExpression(Expression linq)
at System.Data.Objects.ELinq.ExpressionConverter.Conv ert()
at System.Data.Objects.ELinq.ELinqQueryState.GetExecu tionPlan(Nullable`1 forMergeOption)
at System.Data.Objects.ObjectQuery`1.GetResults(Nulla ble`1 forMergeOption)
at System.Data.Objects.ObjectQuery`1.System.Collectio ns.Generic.IEnumerable<T>.GetEnumerator()
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
at System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__3[TResult](IEnumerable`1 sequence)
at System.Data.Objects.ELinq.ObjectQueryProvider.Exec uteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
at System.Data.Objects.ELinq.ObjectQueryProvider.Syst em.Linq.IQueryProvider.Execute[S](Expression expression)
at System.Linq.Queryable.Last[TSource](IQueryable`1 source)
at hesabdan.frmcoustomers.btnback_Click(Object sender, EventArgs e) in D:\abasali\C#‎‎‎\project\hesabdan - Copy (2)\hesabdan\frmcoustomers.cs:line 65
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventAr gs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager. System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.RunDialog(Form form)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at System.Windows.Forms.Form.ShowDialog()
at hesabdan.frmmain.مشتریانToolStripMenuItem_C lick(Object sender, EventArgs e) in D:\abasali\C#‎‎‎\project\hesabdan - Copy (2)\hesabdan\frmmain.cs:line 32
at System.Windows.Forms.ToolStripItem.RaiseEvent(Obje ct key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(Eve ntArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(Eve ntArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(M ouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventIntera ctive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(Event Args e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEven tArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(M ouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Mes sage& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.ToolStripDropDown.WndProc(Mes sage& m)
at System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager. System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at hesabdan.Program.Main() in D:\abasali\C#‎‎‎\project\hesabdan - Copy (2)\hesabdan\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.Run UsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context( Object state)
at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

SabaSabouhi
یک شنبه 10 اسفند 1393, 15:23 عصر
سلام
خیلی ساده هست. فرمان شما باید به یک فرمان SQL تبدیل بشه. ما تو sql فرمانی برای نمایش سطر آخر نداریم.
بجای Last از ترکیب OrderByDescending و First استفاده کن.

var lastItem = MyDb.Customers.OrderByDescending( x=> x.Id ).FirstOrDefault();

صباصبوحی