Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
Dynamics CRM 2011 5
BRH 4
SQL Server 3
#SQLServer 2
ASP.NET 1
#ASP.NET 1
#DOTNET 1
.NET 1
C# 1
BIDS 1

Archive · View All
July 2011 4
August 2011 2

Sanket Shah's Blog

LINQ To Entity and Convert class Methods Problems

Jul 28 2011 2:38PM by Sanket Shah   

While I was working today on one of my projects, as per habits, I used Conversion functions from Convert class and I noticed a peculiarity.

Just sharing this with the readers to keep this in mind:

Ideally, Convert.ToInt32("12") is same as integer 12, but this has a big point to note here.

Let's say my code with Convert.ToInt32() will look like:

 
VAR examBookedSlots = FROM result IN entities.ExamBookings
              WHERE result.UserId == Convert.ToInt32(Session[SessionVariableNames.UserId])
SELECT result;
     

The above code gets compiled perfectly fine - No problems at all. Even "Build Succeeded" message is show in Taskbar.

But when we try to run this code, at runtime, since no named object has been created, an exception will be show that says:

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

Reason: There is no strong reference to int value.

After that, chances are that you will write code like this (this one also gets compiled properly - no errors):

VAR examBookedSlots = FROM result IN entities.ExamBookings
              WHERE result.UserId == (INT)Session[SessionVariableNames.UserId]
SELECT result;
    

This will again show exception that says:

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

Reason: First value of type "object" is returned and then it is typecasted to int. This is, again, not permitted as a temporary anonymous object needs to be created for resolving translation of Data Type.

So, the code that finally succeeds in Build and Run looks like:

    
INT userId = Convert.ToInt32(SESSION[SessionVariableNames.UserId]);
VAR examBookedSlots = FROM result IN entities.ExamBookings
                      WHERE result.UserId == userId
SELECT result;
    

(Linked from my original entry at: http://sanket-shah.com/2011/07/29/linq-to-entity-and-convert-class-methods-problems/)

Tags: C#, .NET, #DOTNET, #ASP.NET, ASP.NET, BRH,


Sanket Shah
305 · 0% · 142
0
Liked
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]