简体   繁体   中英

LINQ statement is returning “Invalid object name” error

I'm completely new to LINQ. I'm just trying to do some easy stuff but I got an error:

Invalid object name 'dbo.grupyTowarowe'.

I could have sworn this worked couple min ago and I didn't change anything.

This is the code:

var papier = from GrupyTowarowe in dbContext.grupyTowarowes
             where GrupyTowarowe.typ == "moneta"
             select new
             {
                 GrupyTowarowe.grupa
             };

dataGridView1.DataSource = papier;

The error is shown on the DataGridView.

Check the schema of 'grupyTowarowe' - it may be different if you've moved to a different database. Eg, your dev database might have it as schema 'dbo' but the next database could create your object under a different schema. If that is the case, use the ALTER SCHEMA command in SSMS.

When you create an anonymous object using "select", you must assign to a field so that it can be referenced.

select new
{
   Grupa = GrupyTowarowe.grupa
}

However, it really looks like all you want is the result. In that case, you do not create an object you simply select the object that is returned.

var papier = from GrupyTowarowe in dbContext.grupyTowarowes
             where GrupyTowarowe.typ == "moneta"
             select GrupyTowarowe;

Correction:

The original syntax for the anonymous class is legal. I have lived and breathed this stuff for over a decade. I just never tried this syntax. You learn new things every day.

Update:

This answer is mostly incorrect, especially since the OP's error is related to the Entity Framework, not Linq. But I am leaving it (feel free to downvote) since there are useful comments that explain why the answer is wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM