从数据库中读取 Entity Framework 上下文。

huangapple go评论52阅读模式
英文:

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&lt;IEnumerable&lt;User&gt;&gt; GetUsersAsync(bool includeFutureUsers, params UserStatus[] statusFilters)
{
        IQueryable&lt;User&gt; query = _context.Users;

        if (statusFilters.Contains(User.All))
        {
            if (!includeFutureUsers)
            {
                DateTime currentUtcTime = DateTime.UtcNow;
                query = query.Where(user =&gt; user.JoinDate &lt;= currentUtcTime);
            }

            return await query.ToListAsync();
        }           

        if (!includeFutureJobs)
        {
            DateTime currentUtcTime = DateTime.UtcNow;
            query = query.Where(user =&gt; user.JoinDate &lt;= currentUtcTime); ----&gt; LINE X
        }

        var users = await query.ToListAsync();

        var filteredUsers = users.Where(user =&gt; 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 =&gt; user.JoinDate &lt;= currentUtcTime &amp;&amp; 
                            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 =&gt; x.ToString()).ToList();

var filteredUsers = await query
    .Where(user =&gt; statuses.Contains(user.Status))
    .ToListAsync();
return filteredUsers;

huangapple
  • 本文由 发表于 2023年6月29日 09:51:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定