Unable to pass an object of type "System.Data.Objects.ObjectQuery`1 [Manager.Data.Channel]" to enter "Manager.Data.Channel"

In a Win7 application, I am trying to update several fields in an ADO.NET database table called "Channel" named EntitySetMapping "Channels", using EntityClient in EF to access SqlServerCe 3.5 (IPManager_DBEntities).

In VSE ID 2010, the code compiles fine, and Intellisense has no complaints. The format of the datatable data channel is referenced below, since the various fields in the line (selected on the Number channel) need to be updated with the information passed to it from code that is not shown for simplicity. None of the things I've been looking for on Google in the last few days solved my Casting dilemma. Using LINQ, I get this RunTime exception:

"Cannot pass an object of type 'System.Data.Objects.ObjectQuery`1 [Manager.Data.Channel]' to enter 'Manager.Data.Channel'.

// Update channel status with information parsed from the data packet. using (IPManager_DBEntities context = new IPManager_DBEntities()) { Channel thisChannelRow = (Channel)(from CE in context.Channels where CE. Number == int.Parse(IDLine[2]) select CE); // Throwing exception after setting up this query: // "Unable to cast object of type 'System.Data.Objects.ObjectQuery`1 // [Manager.Data.Channel]' to type 'Manager.Data.Channel'" // During debug sessions, "thisChannelRow" is null as a result. MessageBox.Show("thisChannelRow. Channel = " + thisChannelRow.Number ); ThisChannel.StatusID = int.Parse(IDLine[5]); ThisChannel.Cycle = int.Parse(IDLine[4]); ThisChannel.Modified = DateTime.Now; context.SaveChanges(); } 

I hope someone has a solution that will help me in this predicament.

+4
source share
3 answers

The LINQ query returns an object of a type child of IEnumerable<T> , not Channel , even if there is only one element in the sequence. To get the actual item, use FirstOrDefault :

 Channel thisChannelRow = (Channel)(from CE in context.Channels where CE. Number == int.Parse(IDLine[2]) select CE).FirstOrDefault(); 

Either First , Single , or SingleOrDefault , if any of them are better suited.

+4
source

I am sure that your first command returns IEnumerable , which is not the same type as the objects it enumerates.

Try adding .First() to the end of your LINQ ala LINQ Select First

This is necessary because your original LINQ statement returns an IEnumerable<Channel> type that does not match the Channel , and therefore you get an incorrect type match. You can think of your original LINQ statement as returning a list of things, but you are trying to put this list in one element. .First() at the end tells the computer to just grab the first item from this list and return it only. Thus, with the updated LINQ statement, you now save the Channel to Channel and therefore no longer perform an incorrect match.

0
source

In my case, I could fix this using the AddRange method.

  Public Function GetMessages(ByVal Issue As Issue) As List(Of Message) Dim Res As New List(Of Message) If Issue IsNot Nothing Then Dim db As New ProjectManagerEntities Res.AddRange(From item As Message In db.Messages Where item.IssueSet_Id = Issue.Id _ Select item) End If Return Res End Function 
0
source

Source: https://habr.com/ru/post/1435574/


All Articles