【问题标题】:Asp.net MVC LINQ error messageAsp.net MVC LINQ 错误信息
【发布时间】:2012-06-22 14:41:47
【问题描述】:

使用 ASP.NET Web-Api,我的控制器中有以下 POST 设置。从 Fiddler 发布到它时,我收到错误消息:

The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities.

...当它到达 var auth = dba.ApiMembers... 行时

    // POST api/Avail
    [BasicAuthentication]
    public HttpResponseMessage PostAvail(Avail[] avail)
    {
        if (ModelState.IsValid)
        {
            // Check if authorised
            var auth = dba.ApiMembers.Where(a => a.hotel_id == 
                   avail[0].HID && a.UserName == User.Identity.Name)
                   .FirstOrDefault();

谁能看出这条线有什么问题?

【问题讨论】:

标签: asp.net asp.net-mvc asp.net-web-api


【解决方案1】:

LINQ2SQL 查询在数据库中运行,无法将User.Identity.Nameavail[0] 转换为数据库命令。您应该将这些值初始化为参数并将简单类型传递给 LINQ 查询。

var hid = avail[0].HID;
var userName = User.Identity.Name;
var auth = dba.ApiMembers.Where(a => a.hotel_id == hid && a.UserName == userName).FirstOrDefault();

【讨论】:

    猜你喜欢
    • 2011-04-04
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-26
    相关资源
    最近更新 更多