Derik Whittaker

Syndication

News


LINQ to Entities oddity with Guid Arrays

Was trying to write very simple Linq statement today (below) and I received the following error

Linq Statement

image

Error Message Received

"LINQ to Entities does not recognize the method 'System.Guid get_Item(Int32)' method, and this method cannot be translated into a store expression."

Of course when I received this I was take back because at first glance I assumed it was trying to convert my GUID to an int and we all know this is not possible.  I then thought maybe I was testing against the wrong column in the database table, but that was not the case.

I have to say I was a bit at a loss, at lease for a second.  I had a theory so I changed my code to look like below:

image

And of course this code WORKED. 

Notice that the ONLY thing I changed was I pushed the Video ID out of the array and into a local variable prior to using Linq.  Turns out that that Linq is not able to use array indexers for GUID arrays, or at least that is my take on why this is not working based on the error.

Long story short is if you get this error try pushing your array item into a local variable rather than trying to access it via an array indexer.

Till next time,

P.S. If you want to learn more about Knockout watch my Pluralsight Course on Knockout JS

 

P.S.S. If you want to learn how to transition your XAML skills to HTML you can watch my course on Html for the XAML Developer


Posted 06-05-2013 6:24 AM by Derik Whittaker

[Advertisement]

Comments

Charlie wrote re: LINQ to Entities oddity with Guid Arrays
on 06-05-2013 9:35 AM

Your original works if you use method syntax:

var foundVideo = ve.ClipVideos.FirstOrDefault(cv => cv.ClipVideoKey == requestInfo.VideoIds[0]);

Derik Whittaker wrote re: LINQ to Entities oddity with Guid Arrays
on 06-05-2013 9:40 AM

@Charlie,

Actually I had that as the original, original syntax and was getting the same error

Charlie wrote re: LINQ to Entities oddity with Guid Arrays
on 06-05-2013 11:44 AM

@Derik,

Weird. I had no trouble with it. Made a sample fully analagous with your snippet: http://pastebin.com/xauSh3gC

You can also swap the FirstOrDefault call with the query syntax and it works, both in 3.5 and 4.0.

I think there's something else going on here -- maybe with whatever Enumerable ve.ClipVideos is

Chris wrote re: LINQ to Entities oddity with Guid Arrays
on 06-06-2013 1:01 PM

Your Linq to Entities query needs to be able to translate directly into a SQL query.

The problem here is that Linq to Entities is trying to pull the whole array into its scope, and then access a member of that array with get_Item.

You'd have the same issue with any array. The correct solution, as you've already figured out, is to assign the array member to a variable first and then use that variable in your Linq to Entities query.

Chris wrote re: LINQ to Entities oddity with Guid Arrays
on 06-06-2013 1:03 PM

Keep in mind that this: System.Guid get_Item(Int32)

is a method signature. get_Item is an array method that takes an Int32 param with the accessor for the array member you're looking for.

System.Guid is the return type, since it is an array of System.Guid objects.

Mike Paterson wrote re: LINQ to Entities oddity with Guid Arrays
on 06-06-2013 8:25 PM

I wouldn't really consider that to be an oddity if you understand LINQ/LINQ providers.  Anything in a LINQ statement should be considered a dynamic operation.  LINQ to Entities attempts to translate these dynamic operations into SQL statements.  SQL obviously doesn't have an equivalent to a C# indexer.  

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)