0

I'm using Entity Framework code first and pulling some data back from our database. In the table I'm accessing is 13 columns that store the number of a items a customer purchased per week for the last 13 weeks as a char field. I have no control over how this data is stored. I need to total the 13 weeks results together to get a combined total, so take week1 + week2 + week3.... = Total Items Purchased over 13 weeks.

In SQL I'd just Cast the char to an Integer and add the values together. But I'm struggling finding a solution to do this in linq.

I tried

(From c in Table
Select New With {.Usage = (Convert.ToInt32(c.week1) + 
                           Convert.ToInt32(c.week2) + etc)}).ToList()

as well trying to use Integer.Parse instead of Convert.ToInt32.

If you have anything that could help me out I'd really appreciate it. Thanks.

3
  • 3
    What happens when you try the code you've got, or Integer.Parse? Commented May 11, 2012 at 6:11
  • you want to add all fields for an item, and require list of all additions right. (similar to Row to column in DB, then Sum()) Commented May 11, 2012 at 6:14
  • The error I'm getting is LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression. Commented May 11, 2012 at 6:15

1 Answer 1

2

EF can't translate Convert.Int32 (and a lot of other LINQ statements...) into SQL. You should just peform the query normally using LINQ to Entities and then perform the conversion outside LINQ.

Sign up to request clarification or add additional context in comments.

2 Comments

I figured this might be the solution but was hoping there was something out there that I'd missed for converting. Thanks for the answer.
I've had the same issue and never found any clean solution, you can use TSQL CAST and execute this manually against the database from EF but I prefer to keep an all code solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.