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

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.



Solution 1:[1]

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
            }
        }
     }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1