是否可以将 await 与使用自定义函数的查询一起使用
Is it possible to use await with query that uses custom function
我想等待此查询,然后再继续执行其他操作。我已经看到几个关于这个主题的问题,但没有一个在查询中使用自定义函数,这使得它有点棘手。甚至可以将 await 与以下查询一起使用。我正在使用 Microsoft Async 包在 .NET4.0 中使用 await-async。(https://www.nuget.org/packages/Microsoft.Bcl.Async/)
var Employees =
(from d in context.Employees
join a in context.Address on d.ID equals a.EmployeeID
select new
{
Employee = d,
Address = a,
})
.AsEnumerable() memory
.Select(x => new Employee
{
Id = x.Employee.Id,
PreferredName = GetPreferredName(x.Employee.FirstName, x.Employee.MiddleName, x.Employee.LastName, x.Employee.Alias),
StreetAddress = x.Address.StreetAddress
})
.ToList();
private string GetPreferredName(string firstName, string middleName, string lastName, string dnsName)
{
if (!string.IsNullOrEmpty(firstName))
return firstName;
else if (!string.IsNullOrEmpty(middleName))
return middleName;
else if (!string.IsNullOrEmpty(lastName))
return lastName;
else if (!string.IsNullOrEmpty(dnsName))
return dnsName;
return "";
}
您可以 await
使用 ToListAsync()
而不是 ToList()
如果您在将查询的第一部分存入内存之前调用它:
var queryEmployees = await
(from d in context.Employees
join a in context.Address on d.ID equals a.EmployeeID
select new
{
Employee = d,
Address = a,
}).ToListAsync();
var employees = Employees.Select(x => new Employee
{
Id = x.Employee.Id,
PreferredName = GetPreferredName(x.Employee.FirstName, x.Employee.MiddleName, x.Employee.LastName, x.Employee.Alias),
StreetAddress = x.Address.StreetAddress
})
.ToList();
您可以使用 ToListAsync
异步获取 EF 查询的结果。
var query = await
(from d in context.Employees
join a in context.Address on d.ID equals a.EmployeeID
select new
{
Employee = d,
Address = a,
})
.ToListAsync();
var Employees = query
.Select(x => new Employee
{
Id = x.Employee.Id,
PreferredName = GetPreferredName(x.Employee.FirstName, x.Employee.MiddleName, x.Employee.LastName, x.Employee.Alias),
StreetAddress = x.Address.StreetAddress
})
.ToList();
我想等待此查询,然后再继续执行其他操作。我已经看到几个关于这个主题的问题,但没有一个在查询中使用自定义函数,这使得它有点棘手。甚至可以将 await 与以下查询一起使用。我正在使用 Microsoft Async 包在 .NET4.0 中使用 await-async。(https://www.nuget.org/packages/Microsoft.Bcl.Async/)
var Employees =
(from d in context.Employees
join a in context.Address on d.ID equals a.EmployeeID
select new
{
Employee = d,
Address = a,
})
.AsEnumerable() memory
.Select(x => new Employee
{
Id = x.Employee.Id,
PreferredName = GetPreferredName(x.Employee.FirstName, x.Employee.MiddleName, x.Employee.LastName, x.Employee.Alias),
StreetAddress = x.Address.StreetAddress
})
.ToList();
private string GetPreferredName(string firstName, string middleName, string lastName, string dnsName)
{
if (!string.IsNullOrEmpty(firstName))
return firstName;
else if (!string.IsNullOrEmpty(middleName))
return middleName;
else if (!string.IsNullOrEmpty(lastName))
return lastName;
else if (!string.IsNullOrEmpty(dnsName))
return dnsName;
return "";
}
您可以 await
使用 ToListAsync()
而不是 ToList()
如果您在将查询的第一部分存入内存之前调用它:
var queryEmployees = await
(from d in context.Employees
join a in context.Address on d.ID equals a.EmployeeID
select new
{
Employee = d,
Address = a,
}).ToListAsync();
var employees = Employees.Select(x => new Employee
{
Id = x.Employee.Id,
PreferredName = GetPreferredName(x.Employee.FirstName, x.Employee.MiddleName, x.Employee.LastName, x.Employee.Alias),
StreetAddress = x.Address.StreetAddress
})
.ToList();
您可以使用 ToListAsync
异步获取 EF 查询的结果。
var query = await
(from d in context.Employees
join a in context.Address on d.ID equals a.EmployeeID
select new
{
Employee = d,
Address = a,
})
.ToListAsync();
var Employees = query
.Select(x => new Employee
{
Id = x.Employee.Id,
PreferredName = GetPreferredName(x.Employee.FirstName, x.Employee.MiddleName, x.Employee.LastName, x.Employee.Alias),
StreetAddress = x.Address.StreetAddress
})
.ToList();