在实体框架中使用左联接进行过滤

时间:2020-02-01 15:12:49

标签: sql entity-framework asp.net-core

我在ASP.NET Core API项目中有UserContactContactOfUser实体。我想根据这些表上的输入来过滤用户。

我的实体类如下:

    public class User
    {
        public int Id { get; set; }

        [MaxLength(50)]
        public string Name { get; set; }

        [MaxLength(50)]
        public string Surname { get; set; }

        [MaxLength(60)]
        public string Username { get; set; }
}

public class Contact
{
    public int Id { get; set; }

    [MaxLength(50)]
    public string Value{ get; set; }
}

public class ContactOfUser
{
    public int Id { get; set; }

    public int UserId { get; set; }
    [ForeignKey(nameof(UserId))]
    public User User { get; set; }

    public int ContactId { get; set; }
}

我想基于此FilterModel对象来过滤用户:

public class FilterModel
{
     public string Name { get; set; }
     public string Surname { get; set; }
     public string Username { get; set; }
     public List<int> ContactId { get; set; }
}

考虑到当数据被接受为null时不应用特殊过滤器数据,我如何使用Linq方法在Entity Framework中进行此过滤过程?

我做了类似的方法,但是它不能正常工作:

List<User> GetFilteredUsers(FilterModel filter)
{
    var query1 = dbContext.Users
                          .Where(u => u.Name.Contains(filter.Name ?? string.Empty) &&
                                      u.Surname.Contains(filter.Surname ?? string.Empty) &&
                                      u.Username.Contains(filter.Username ?? string.Empty));

    var query2 = from u in query1
                 join cu in dbContext.ContactOfUsers on u.Id equals cu.UserId
                 into res
                 from item in res.DefaultIfEmpty()
                 where filter.Contacts.Contains(item.ContactId)
                 select new InitialUserModel
                         {
                             Id = u.Id,
                             Name = u.Name,
                             Surname = u.Surname,
                             Username = u.Username
                         };
}

1 个答案:

答案 0 :(得分:1)

您可以通过这种方式实现

  • 使用GroupBy相应地获取userIdContactIds
var userContactIds = _dbContext.ContactOfUser.GroupBy(p => p.UserId).Select(g => new { UserId = g.UserId,  ContactIds = g.Select(p => p.ContactId).ToList() });
  • 获取结果:
var result = _dbContext.User.Select(p => new FilterModel 
{ 
   Name = p.Name, Surname = p.Surname, Username = p.Username, 
   ContactId = userContactIds.Where(c => p.Id == c.UserId).ToList() 
});

已更新

List<User> GetFilteredUsers(FilterModel filter)
{
     return (from u in _dbContext.User
            join c in _dbContext.ContactOfUser on u.Id equals c.UserId
            join fContactId in filter.ContactId on c.ContactId equals fContactId 
            where u.Name == filter.Name && u.Surname == filter.Surname && u.Username == filter.Username 
            select u).ToList();
}