如何使用异步和LINQ等待? [英] How to use async and await in LINQ?

查看:293
本文介绍了如何使用异步和LINQ等待?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来ASP.NET和正试图在一个样本项目。我知道这是一个愚蠢的QS,但请多多包涵。我的code下面,即使有在SQL查询中返回了多行只返回一个结果。我认识到,FirstOrDefaultAsync只返回的第一个元素。我会经过这里的文档:<一href=\"https://msdn.microsoft.com/en-us/library/system.data.entity.queryableextensions(v=vs.113).aspx\" rel=\"nofollow\">https://msdn.microsoft.com/en-us/library/system.data.entity.queryableextensions(v=vs.113).aspx但我找不到用什么来代替FirstOrDefaultAsync,将返回不只是第一行,但一切都符合我的SQL条件。

I'm new to ASP.NET and was trying to work on a sample project. I know this is a silly qs but please bear with me. My code below is returning only one result even when there are multiple rows returned in the SQL query. I realize that FirstOrDefaultAsync returns only the first element. I was going through the documentation here: https://msdn.microsoft.com/en-us/library/system.data.entity.queryableextensions(v=vs.113).aspx but I couldn't find what to use instead of FirstOrDefaultAsync that would return not just the first row but everything that matches my SQL condition.

这是我的code迄今:

This is my code so far:

    [ResponseType(typeof(Region))]
    public async Task<IHttpActionResult> GetRegion(int id)
    {

        var region_id = from sr in db.ServiceToRegions
                           where sr.Service_ID == id
                           select sr.Region_ID;

        var region = await db.Regions.Select(r =>
            new 
            {
                Region_ID = r.Region_ID,
                Region_Name = r.Region_Name
            }).FirstOrDefaultAsync(r => region_id.Contains(r.Region_ID)); //QQQ

        if (region == null)
        {
            return NotFound();
        }

        return Ok(region);
    }

我应该怎么使用FirstOrDefaultAsync代替?

What should I be using instead of FirstOrDefaultAsync?

推荐答案

您应该使用后ToListAsync()其中:

You should use ToListAsync() after Where:

var region = await db.Regions
    .Select(r =>
        new Region
        {
            Region_ID = r.Region_ID,
            Region_Name = r.Region_Name
        }
    )
    .Where(r => region_id.Contains(r.Region_ID))
    .ToListAsync();

https://msdn.microsoft.com /en-us/library/dn220257(v=vs.113).aspx

请注意新区域,以解决您的匿名类型的问题进行序列化(我不能斯科特的回答评论)。

Note "new Region" to solve your anonymous type problem for serialization (I cannot comment on Scott's answer).

UDPATE

由于您尝试加载部分的模型(只有2个属性),您需要使用视图模型是这样的:

Because you try to partially load a model (only 2 properties), you need to use a ViewModel like this:

public class RegionVM
{
    public int Region_ID { get; set; }
    public string Region_Name { get; set; }
}

然后,修改code此:

Then change your code for this:

var region = await db.Regions
    .Select(r =>
        new RegionVM
        {
            Region_ID = r.Region_ID,
            Region_Name = r.Region_Name
        }
    )
    .Where(r => region_id.Contains(r.Region_ID))
    .ToListAsync();

不要忘了更新您的响应类型!

Don't forget to update your response type!

这篇关于如何使用异步和LINQ等待?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆