0

Code :

var page = (from p in dbContext.Page
            where p.Id== Id
            select new PageObject
            {
                PageID = p.PageID,
                CategoryId = p.attr.Any(a => a.Attribute == "CategoryId") ? 
                p.attr.Where(a => a.Attribute == "CategoryId").FirstOrDefault().Value : 0 // How do I convert the Value to Int?
            }).FirstOrDefault();

Problem :

We are using EF in existing application where in the above code it is initializing PageObject (with lots of properties which I have deleted for simplicity).The CategoryId is an integer and I am trying to figure out how to convert the Value to int, I am unable to use int.Parse on the LINQ query as it throws an error :

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

Note: I cannot avoid using short hand property initialization, and this needs to be done in the LINQ query itself.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Murtaza Mandvi
  • 10,708
  • 23
  • 74
  • 109
  • Why do you need to if it is already an integer? .NET would be able to parse that automatically as far as I am concerned. – Hozikimaru Jul 21 '15 at 15:24
  • Why is CategoryId mapped as a string when it's clearly an integer? Should it change types in the database, or should the Id be a string? – Bas Jul 21 '15 at 15:24
  • In the DB its a sort of a hastable so the same field can store strings and intgers, depending on the key we decide if its supposed to be an int or not, can't change that structure anymore. – Murtaza Mandvi Jul 21 '15 at 15:25
  • @Bas The post you mentioned is checking for internal conditional properties to be int or not, my post is referring to the entire result from the LINQ to be converted to int, seems like a non duplicate to me ! – Murtaza Mandvi Jul 21 '15 at 15:28
  • The root of the problem in both cases is converting a string to an integer in the database using entity framework. That is the same in both, even though you use the result differently! – Bas Jul 21 '15 at 15:29
  • If `Value` is an integer, why not just try a straight cast, aka `(int)(p.attr.Where(a => a.Attribute == "CategoryId").FirstOrDefault().Value)`? Unless I'm misunderstanding and its a string? – Ron Beyer Jul 21 '15 at 15:30
  • @RonBeyer Straight cast does not work either, throws an exception – Murtaza Mandvi Jul 21 '15 at 15:30
  • Whats the exception Cast throws? And note, if there are strings, why would you want to do the conversion at all times? Of course you wont be able to convert it if is not a valid integer. – Hozikimaru Jul 21 '15 at 15:31
  • Can someone please re-open this question as it does not seem duplicate to me... – Murtaza Mandvi Jul 21 '15 at 15:31
  • @SurgeonofDeath (int)(p.attr.Where(a => a.Attribute == "CategoryId").FirstOrDefault().Value) This does not even compile and simply says "Cannot Convert type 'string' to 'int' – Murtaza Mandvi Jul 21 '15 at 15:33
  • @MurtazaMandvi try, Cast().FirstOrDefault(), instead of having the (int) at the beginning. – Hozikimaru Jul 21 '15 at 15:34
  • p.attr.Where(a => a.Attribute == "CategoryId").Cast().FirstOrDefault().Value - The Value object is does not exist in this case :) – Murtaza Mandvi Jul 21 '15 at 15:35
  • what happens when you remove the value? – Hozikimaru Jul 21 '15 at 15:35
  • That dosent work because the Value is the actual property fromt he DB that holds my value : Unable to cast the type 'PageAttr' to type 'System.Int32'. LINQ to Entities only supports casting EDM primitive or enumeration types. – Murtaza Mandvi Jul 21 '15 at 15:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83889/discussion-between-surgeon-of-death-and-murtaza-mandvi). – Hozikimaru Jul 21 '15 at 15:39

0 Answers0