1

I'm working on an MVC web app using Entity Framework 6. I can't figure out why I can query with LINQ but context.Database.SqlQuery() doesn't work and always returns an invalid object name error for the table.

This works:

db.MatchHistory.Where(u => u.id == sumID).ToList().Count()

This doesn't. I also tried tblMatchHistory and dbo.tblMatchHistory.

db.MatchHistory.SqlQuery("SELECT * FROM [dbo].[tblMatchHistory] WHERE id = '" + sumID + "'").ToList().Count()

What am I missing? Or should I just not be using SqlQuery()?

1 Answer 1

1

You should use count() in SQL like

int nCount= db.MatchHistory.SqlQuery("SELECT COUNT(id) as id FROM [dbo].[tblMatchHistory] WHERE id = '" + sumID + "'");
Sign up to request clarification or add additional context in comments.

1 Comment

That's true, I should have, but I have another SqlQuery() line without a count and it has the same problem.

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.