Question: I building my first application with c# and sp.net MVC 5, so far so good :)

Now I have a problem, we using 2 User Tables, first one contains the username, other the user data.

```
string user = User.Identity.Name;

        var data = db.FE_Benutzer;
        var collection = data.Where(o => o.Benutzername == user).Select(x => new
        {
            id = x.ID,
            name = x.Name,
            hauptbereiche = x.Hauptbereich.ToList()
        });

        var dataHauptbereich = db.Hauptbereich;

        var collectionHauptbereich = dataHauptbereich.Where(o => collection.ElementAt(0).hauptbereiche.Contains(o)).Select(x => new
        {
            id = x.ID,
            name = x.Name
        });

        return Json(collectionHauptbereich, JsonRequestBehavior.AllowGet);

```

I getting this error

> 
> LINQ to Entities does not recognize the method '<>f\_\_AnonymousType6`3[System.Int32,System.String,System.Collections.Generic.List`1[scorring.Models.Hauptbereich]] ElementAt[<>f\_\_AnonymousType6`3](System.Linq.IQueryable`1[<>f\_\_AnonymousType6`3[System.Int32,System.String,System.Collections.Generic.List`1[scorring.Models.Hauptbereich]]], Int32)' method, and this method cannot be translated into a store expression.
> 
> 
> 

hauptbereiche = x.Hauptbereich.ToList()

contains a list of ids where the user have premission to.

When I fetching the data

dataHauptbereich.Where

I wont to include only the ids I have in the list

how is this possible?

Answer: 
Entity Framework doesn't know how to turn `ElementAt` into SQL. See this answer for more information: [Getting the first result from a LINQ query - why does ElementAt<T>(0) fails when First<T>() succeeds?](https://stackoverflow.com/questions/2650150/getting-the-first-result-from-a-linq-query-why-does-elementatt0-fails-when)
I'm having a bit of a time deciphering exactly what you're trying to achieve with your code here, but it looks to me like your simply querying `Hauptbereich`s that belong to a particular user. Your first query selects an anonymous object composed of `id`, `name` and `hauptbereiche`, but of these you only ever use the `hauptbereiche` property. Then, in your second query, you merely selecting `Hauptbereich`s that match an item in this `hauptbereiche` property's collection. Actually, here, you're only comparing values from the first item in the original collection, which begs the question of why you're selecting anything other than the first item. That, and this second query is entirely redundant because if the items match that means you already had the items in the first place. You could get the same info directly from `collection.ElementAt(0).hauptbereiche` without issuing the second query.

So, here's a couple of simpler options:

1. If you're trying to get all the `Hauptbereich`s that belong to all the `FE_Benutzer`s where `Benutzername == user` then just do:

```
var collectionHauptbereich = db.FE_Benutzer.Where(m => m.Benutzername == user)
    .Include(m => m.Hauptbereich)
    .SelectMany(m => m.Hauptbereich);

```
2. If you want just the first `FE_Benutzer` item's `Hauptbereich`s, then do:

```
var benutzer = db.FE_Benutzer.Where(m => m.Benutzername == user)
    .Include(m => m.Hauptbereich)
    .FirstOrDefault();
var collectionHauptbereich = benutzer != null
    ? benutzer.Hauptbereich.ToList()
    : new List<Hauptbereich>();

```