Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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
Linq 在ASP.NET核心MVC中使用选择查询时出现InvalidOperationException_Linq_Asp.net Core_Model View Controller_Linq To Sql_Entity Framework Core - Fatal编程技术网

Linq 在ASP.NET核心MVC中使用选择查询时出现InvalidOperationException

Linq 在ASP.NET核心MVC中使用选择查询时出现InvalidOperationException,linq,asp.net-core,model-view-controller,linq-to-sql,entity-framework-core,Linq,Asp.net Core,Model View Controller,Linq To Sql,Entity Framework Core,我正在创建一个表,该表将显示SQL数据库中的某些列。我发现了错误 InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List, but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.

我正在创建一个表,该表将显示SQL数据库中的某些列。我发现了错误

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List, but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IEnumerable
我尝试运行原始sql查询(使用.FromSqlRaw),但在我的视图中返回空模型值。我理解这个错误,并且将我的查询设置为列表并返回IEnumerable是不可能的,但是我在网上没有发现成功。请让我知道我能改进什么。谢谢

我的看法

@model IEnumerable<MyApp.Models.BookModel>
            <table>
                <thead>
                    <tr>
                        <th>
                            @Html.DisplayNameFor(model => model.BookName)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.Author)
                        </th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var item in Model)
                    {
                        <tr>
                            <td>
                                @Html.DisplayFor(modelItem => item.BookName)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Author)
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
@model IEnumerable
@DisplayNameFor(model=>model.BookName)
@DisplayNameFor(model=>model.Author)
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.BookName)
@DisplayFor(modeleItem=>item.Author)
}
我的控制器

        [HttpGet("[action]")]
        [Route("/Books/Index")]
        public async Task<IActionResult> Index()
        {
            try
            {
                var data = _context.BookModel
                    .Where(x => x.Id == 10)
                    .Select(x => new { x.BookName, x.Author })
                    .AsNoTracking()
                    .ToListAsync();

                return View(await data);
            }
            catch (Exception)
            {

                throw;
            }
        }
[HttpGet(“[action]”)
[路线(“/Books/Index”)]
公共异步任务索引()
{
尝试
{
var data=\u context.BookModel
.其中(x=>x.Id==10)
.Select(x=>new{x.BookName,x.Author})
.AsNoTracking()
.ToListAsync();
返回视图(等待数据);
}
捕获(例外)
{
投掷;
}
}

旁注:设置id=10用于测试。

您可以发送MyApp.Models.BookModel的列表,将查询更改为:

var data = _context.BookModel
    .Where(x => x.Id == 10)
    .Select(x => new BookModel { BookName = x.BookName, Author = x.Author })
    .AsNoTracking()
    .ToListAsync();


我希望这对您有所帮助。

new{x.BookName,x.Author}
创建了一个新的匿名类型,它与
BookModel
@KirkLarkin不同好的,很高兴知道,谢谢您的回复。你对我如何修改代码有什么建议吗?
var data = _context.BookModel
    .Where(x => x.Id == 10)
    .Select(x => new { x.BookName, x.Author })
    .AsNoTracking()
    .Select(x => new BookModel { BookName = x.BookName, Author = x.Author })
    .ToListAsync();