不能在 Linq 语句中使用 select

对不起,如果这是一个愚蠢的问题,但我是 LINQ 的新手。我有一个使用 Dapper 使用 async/await 逻辑查询数据库的服务。服务中的方法我可以在 linq 中使用 SELECT 但在控制器中此选项不允许我使用?我只有“ToSelectListItems”而不是 SELECT ..我在这里错过了什么?


服务:


    public async Task<IEnumerable<AwardWinResult>> GetAwardWinnersAsync(bool useCache = true)

    {

        var data = await awardWinnersRepository.GetWinnersAsync();

        return data;;

    }

控制器:


    public async Task<ActionResult> Index()

    {

        var awardWinners = await awardWinnersService.GetAwardWinnersAsync();


        var viewModel = new AwardWinnersWrapperVM

        {

            AwardWinners = await awardWinners.SelectMissingHere?(x => new AwardWinnersViewModel

            {

                AwardId = x.AwardId

            })

        };


        return View(awardWinners);

    }

存储库:


    public async Task<IEnumerable<AwardWinResult>> GetWinnersAsync()

    {

        using (var conn = new System.Data.SqlClient.SqlConnection(Proj.DataContext.ConnectionString))

        {

            await conn.OpenAsync();


            var data = await conn.QueryAsync<AwardWinResult>(@"

                SELECT aw.[WinnerId], aw.[AwardId], aw.[AwardCategoryId] as CategoryId, a.[Name] as AwardName, ac.[Name] as CategoryName, a.[StartDate], a.[EndDate], a.[LaunchDate]

                FROM [Proj].[AwardWinners] aw

                LEFT JOIN [Proj].[AwardCategories] ac on aw.AwardCategoryId = ac.Id

                LEFT JOIN [Proj].[Awards] a on aw.AwardId = a.Id

                LEFT JOIN [Proj].[AwardJurisdictionGroups] ajg on ac.[JurisdictionGroupId] = ajg.[Id]

                LEFT JOIN [Proj].[AwardWorkareaGroups] awg on ac.[WorkareaGroupId] = awg.[Id]

                ");


            return data.ToList();

        }

    }



www说
浏览 174回答 1
1回答

摇曳的蔷薇

await看来您部分不需要SelectMissingHere:public async Task<ActionResult> Index(){&nbsp; &nbsp; var awardWinners = await awardWinnersService.GetAwardWinnersAsync();&nbsp; &nbsp; var viewModel = new AwardWinnersWrapperVM&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; AwardWinners = awardWinners.Select(x => new AwardWinnersViewModel&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AwardId = x.AwardId&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; };&nbsp; &nbsp; return View(awardWinners);}你也可以添加AwardWinnersWrapperVM代码吗?还要检查using System.Linq控制器中的指令。
打开App,查看更多内容
随时随地看视频慕课网APP