15

I've read some tutorials with entity framework 6...

The basics are easy.

using (var context = new MyContext())
{
    User u = context.Users.Find(1);
}

But how to use "Where" or something else on the "DbSet" with the users?

public class MyContext : DbContext
{
    public MyContext()
        : base("name=MyContext")
    {
        //this.Database.Log = Console.Write;
    }

    public virtual DbSet<User> Users { get; set; }
}

Users

[Table("User")]
public class User : Base
{
    public Guid Id { get; set; }

    [StringLength(100)]
    public string Username { get; set; }
}

And thats the problem which doesnt work.

string username = "Test";
using (var context = new MyContext())
{
    User u = from user in context.Users where user.Username == username select user;
}

Error: There was no implementation of the query pattern for source type 'DbSet'. 'Where' is not found. Maybe a reference or a using directive for 'System.Link' is missing.

If i try to autocomplete the methods there are none.

VS2013

Why it doesnt works? :(

// Edit: Adding System.Linq to the top of the file changes the functions of the problem above so that i havent a problem anymore.

But why the where is wrong now?

The type "System.Linq.IQueryable<User>" cant converted into "User" explicit. There already exists an explicit conversion. (Possibly a cast is missing)

above doesnt work, bottom works

3
  • 6
    is System.Linq reference added to namespace? Commented Oct 24, 2014 at 12:12
  • Wtf!? Adding "using System.Linq" at the top of the file where i use the string above fixed the problem... Funny thing... Can you recheck the question. I added a problem with is with System.Linq, too. Thanks!! Commented Oct 24, 2014 at 12:24
  • 2
    The query is returning a type of System.Linq.IQueryable<User> and you are trying to put it in a variable of type User. I guess you need First() on the end of the query. Commented Oct 24, 2014 at 12:29

2 Answers 2

55

Thanks to @Grant Winney and @Joe.

Adding using System.Linq; to the namespace/top of the document where i'm tring the code above fixed the problem.

And using the line above it works for the first item of the list.

User user = (select user from context.Users where user.Username == username select user).First();
Sign up to request clarification or add additional context in comments.

1 Comment

You saved my hour even 2019 with Entity Framework Core 3 preview
2

The (second) problem is in what you expect:

User u = from user in context.Users where user.Username == username select user;

You're expecting a single element. But a Where clause returns a list (IEnumerable) of items. Even if there's only one entity that fits the where clause, it will still return a list (with a single item in it).

If you want a single item, you need to either take the .First() or the .Single() item of that list.

Some considerations:

  • Either method I just mentioned can take a clause similar to how the where clause works. This means you can skip the Where and put the clause straight in this method.
  • Single only works if only one element exists (fitting the clause). If two elements occur, an exception will be thrown.
  • First works similar to Single, but it will not throw an exception if multiple items exist (fitting the clause). It will simply return the first element it finds (fitting the clause).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.