Could not find an implementation of the query pattern for source type 'System.Data.Entity.DbSet'

c# entity-framework linq

Question

I'm using Entity Framework for the first time, but it seems not working as expected.

I have this code:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;

public static class QueryClass
{
    public static void Query()
    {
        using (var context = new MyDbEntities())
        {
            DbSet<MyTable> set = context.Tables;
            var query = from val in set select value; 

        }
    }
}

On the query line (exactly the "set" variable is underlined in red) I get the error:

Could not find an implementation of the query pattern for source type 'System.Data.Entity.DbSet'.'Select' not found. Missing a reference or an using directive for 'System.Linq'

MyDbEntities is auto-generated by Entity Framework in a Database-First approach, context.Tables is a DbSet, so it should be able to use Linq, which has been added through the using directive. In order to avoid misurderstantings, within this class I find the following:

public virtual DbSet<MyTable> Tables { get; set; }

What am I missing in order to make the select work?

Thank you.

1
23
8/10/2015 11:22:50 AM

Accepted Answer

you will need to add reference to System.Data.Linq

System.Data.Linq is LINQ-SQL specific (DataContext, etc)

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Linq;
using System.Linq;

public static class QueryClass
{
    public static void Query()
    {
        using (var context = new MyDbEntities())
        {

            IQueryable<MyTable> qTable= from t in context.Tables
                                        select t; // can you confirm if your context has Tables or MyTables?
            Console.WriteLine("Table Names:");
            foreach (var t in qTable)
            {
                Console.WriteLine(t.Name);//put the relevant property instead of Name
            }
        }
     }
}
36
7/6/2015 11:31:07 AM

Popular Answer

Just added a reference using System.Linq; and worked fine as already mentioned above.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow