英文:
Read from database Entity Framework Context
问题
You can update line X as follows to get users with Status
included in statusFilters
:
query = query.Where(user => user.JoinDate <= currentUtcTime && statusFilters.Contains((UserStatus)Enum.Parse(typeof(UserStatus), user.Status)));
However, if you encounter an error like the one you mentioned, you can try using AsEnumerable()
to switch to client evaluation, like this:
query = query.AsEnumerable().Where(user => user.JoinDate <= currentUtcTime && statusFilters.Contains((UserStatus)Enum.Parse(typeof(UserStatus), user.Status)));
This should help you resolve the issue you mentioned.
英文:
I use an EF DbContext
and have the following code to read users from a SQL table with specific statuses:
public async Task<IEnumerable<User>> GetUsersAsync(bool includeFutureUsers, params UserStatus[] statusFilters)
{
IQueryable<User> query = _context.Users;
if (statusFilters.Contains(User.All))
{
if (!includeFutureUsers)
{
DateTime currentUtcTime = DateTime.UtcNow;
query = query.Where(user => user.JoinDate <= currentUtcTime);
}
return await query.ToListAsync();
}
if (!includeFutureJobs)
{
DateTime currentUtcTime = DateTime.UtcNow;
query = query.Where(user => user.JoinDate <= currentUtcTime); ----> LINE X
}
var users = await query.ToListAsync();
var filteredUsers = users.Where(user => statusFilters.Contains((User)Enum.Parse(typeof(UserStatus), user.Status)));
return filteredUsers;
}
}
public enum UserStatus
{
All = 0,
Young = 1,
Adult = 2,
Men = 3,
Women = 4,
Toddler = 5
}
public class User
{
public User()
{
}
public string Status { get; set; }
}
As you see here, UserStatus
is of type Enum
and User.Status
is of type string.
Is it possible to update line X to get users with Status
included in statusFilters
?
I tried:
query = query.Where(user => user.JoinDate <= currentUtcTime &&
statusFilters.Contains((User)Enum.Parse(typeof(UserStatus), user.Status)));
But that throws an error
> Could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'
How do I fix this?
答案1
得分: 1
如果在数据库/用户实体中将状态存储为字符串,那么在Linq查询中,使用字符串来表示所需的状态,而不是尝试转换数据。在筛选之前不要使用 ToList
将用户加载,这将加载所有用户,并且只会在 ToList
调用之前执行筛选。
var statuses = statusFilters.Select(x => x.ToString()).ToList();
var filteredUsers = await query
.Where(user => statuses.Contains(user.Status))
.ToListAsync();
return filteredUsers;
英文:
If the status is stored as a String in the database / User Entity then in the Linq query, use strings for the desired statues rather than trying to convert the data. Don't ToList
the users prior to filtering, this will load all users with only whatever filtering you've done before the ToList
call.
var statuses = statusFilters.Select(x => x.ToString()).ToList();
var filteredUsers = await query
.Where(user => statuses.Contains(user.Status))
.ToListAsync();
return filteredUsers;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论