Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.net MVC在列表中的位置_C#_Asp.net_Asp.net Mvc 5 - Fatal编程技术网

C# asp.net MVC在列表中的位置

C# asp.net MVC在列表中的位置,c#,asp.net,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc 5,我用c#和sp.net MVC 5构建了我的第一个应用程序,到目前为止还不错:) 现在我有一个问题,我们使用2个用户表,第一个包含用户名,另一个包含用户数据 string user = User.Identity.Name; var data = db.FE_Benutzer; var collection = data.Where(o => o.Benutzername == user).Select(x => new {

我用c#和sp.net MVC 5构建了我的第一个应用程序,到目前为止还不错:)

现在我有一个问题,我们使用2个用户表,第一个包含用户名,另一个包含用户数据

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);
我得到了这个错误

LINQ to Entities无法识别方法“f_uAnonymousType6
3[System.Int32,System.String,System.Collections.Generic.List
1[scorring.Models.Hauptbreich]]ElementAt[f_uAnonymousType6
3](System.LINQ.IQueryable
1[f_uAnonymousType6
3[System.Int32,System.String,System.Collections.Generic.List
1[scorring.Models.hauptbreich]],Int32)“方法,此方法无法转换为存储表达式

hauptbreiche=x.hauptbreich.ToList()

包含用户有预授权访问的ID列表

当我提取数据时

Datahauptbreich.在哪里

我不想在列表中只包括我的ID

这怎么可能呢?

试试看

dataHauptbereich.Where(o => collection.ElementAt(0).hauptbereiche.Any(h => h.ID == o.ID))


实体框架不知道如何将
ElementAt
转换为SQL。有关更多信息,请参阅此答案:

我花了一点时间来准确地解释您在这里试图用代码实现的目标,但在我看来,这就像您简单地查询属于特定用户的
hauptbreich
。您的第一个查询选择了cts一个由
id
name
hauptbreiche
组成的匿名对象,但其中您只能使用
hauptbreiche
属性。然后,在第二个查询中,您只需选择与该
hauptbreiche
属性集合中的项目匹配的
hauptbreich
即可。实际上,在这里,您是e仅比较原始集合中第一个项目的值,这就引出了一个问题:为什么选择第一个项目以外的任何项目。这和第二个查询是完全冗余的,因为如果项目匹配,这意味着您首先已经有了项目。您可以直接从
集合中获得相同的信息。ElementAt(0).hauptbreiche
而不发出第二个查询

因此,这里有两个更简单的选择:

  • 如果您试图获取属于所有
    FE_Benutzer
    s其中
    Benutzername==user
    的所有
    hauptbreich
    s,那么只需执行以下操作:

    var collectionHauptbereich = db.FE_Benutzer.Where(m => m.Benutzername == user)
        .Include(m => m.Hauptbereich)
        .SelectMany(m => m.Hauptbereich);
    
  • 如果您只需要第一个
    FE_Benutzer
    项目的
    Hauptbreich
    s,请执行以下操作:

    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>();
    
    var benutzer=db.FE\u benutzer.Where(m=>m.Benutzername==user)
    .包括(m=>m.Hauptbreich)
    .FirstOrDefault();
    var collectionhauptbreich=benutzer!=null
    ?benutzer.Hauptbreich.ToList()
    :新列表();
    

  • Where(o=>somelist.Contains(o))
    @emodendroket我已经更新了我的问题,新的我有一个错误…仍然相同的错误LINQ to Entities无法识别方法'f_AnonymousType6
    3[System.Int32,System.String,System.Collections.Generic.List
    1[scorring.Models.Hauptbreich]]ElementAt[f_AnymousType6
    3](System.Linq.IQueryable
    1[f_uAnonymousType6
    3[System.Int32,System.String,System.Collections.Generic.List
    1[scorring.Models.Hauptbreich]],Int32)”方法,并且此方法无法转换为存储表达式。
    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>();