[英]LINQ select new Async/Await
I currently have a asynchronous query as follows which is fine and allows me to use the FirstOrDefaultAsync / ToListAsync methods. 我目前有一个异步查询,如下所示,这很好,允许我使用FirstOrDefaultAsync / ToListAsync方法。
public async Task<X> FindXAsync(int x)
{
var q = from c in context.X
where c.Id == x
select c;
return await q.FirstOrDefaultAsync();
}
However I am attempting to extend that query to select into a new class 但是我试图扩展该查询以选择一个新类
public async Task<XClass> FindXAsync(int x)
{
var q = from c in context.X
where c.Id == x
select new XClass (
c.Id,c.Header .........
);
return await q.FirstOrDefaultAsync();
}
For the above you can no longer use the FirstOrDefaultAsync() only FirstOrDefault(), I was wondering what would be the most efficient way to get this functionality into an asynchronous method. 对于以上内容,您不能再仅使用FirstOrDefaultAsync()了,我想知道将这种功能转换为异步方法的最有效方法是什么。 Thanks, Chris
谢谢克里斯
In this case, the easiest answer is probably just to do the (asynchronous) first selection, and then create the type you need: 在这种情况下,最简单的答案可能就是先选择(异步), 然后创建所需的类型:
public async Task<XClass> FindXAsync(int x)
{
var q = from c in context.X
where c.Id == x
select c;
var c = await q.FirstOrDefaultAsync();
return new XClass (
c.Id,c.Header .........
);
}
尝试使用通用类型化参数:
return await q.FirstOrDefaultAsync<XClass>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.