我在数据库中有一些表,它们将被连接到一个Log
表中。
这是一张桌子,然后,我展示了Log
张桌子
用户实体:
public class User : BaseEntity
{
public User()
{
Beneficiaries = new HashSet<Beneficiary>();
Applicants = new HashSet<Applicant>();
UserItemCategories = new HashSet<UserItemCategory>();
UserPropertyCategories = new HashSet<UserPropertyCategory>();
Payments = new HashSet<Payment>();
}
[Required]
public Role Role { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public string Mobile { get; set; }
[Required]
public string Username { get; set; }
[Required]
public string Phone { get; set; }
[Required]
public string Address { get; set; }
[Required]
public string Password { get; set; }
public double FixedSalary { get; set; }
public DateTime DateOfPay { get; set; }
public virtual ICollection<Beneficiary> Beneficiaries { get; set; }
public virtual ICollection<Applicant> Applicants { get; set; }
public virtual ICollection<UserItemCategory> UserItemCategories { get; set; }
public virtual ICollection<UserPropertyCategory> UserPropertyCategories { get; set; }
public virtual ICollection<Payment> Payments { get; set; }
}
BaseEntity 类:
public abstract class BaseEntity
{
public string Id { get; set; }
public DateTime DateTime { get; set; }
}
日志实体:
public class Log : BaseEntity
{
public TrackTypeEnum Type { get; set; }
[Required]
public string CreatorId { get; set; }
[Required]
public string EntityId { get; set; }
}
在Log
表中,我想将entityId
加入User
表的ID中
我写了一个代码来处理这个问题:
public static IQueryable<TEntity> Filtered<TEntity>(this IQueryable<TEntity> entities, IQueryable<Log> logDbSet) where TEntity : BaseEntity
{
var entitiesWithAttachedLogs = entities
.GroupJoin(logDbSet, entity => entity.Id, log => log.EntityId, (entity, logs) => new
{
Entity = entity,
Logs = logs
});
var groupedEntities = entitiesWithAttachedLogs.GroupBy(key => key.Entity.Id, value => new
{
value.Entity,
Logs = value.Logs.DefaultIfEmpty()
});
var shrinkGroup = groupedEntities.SelectMany(group => group, (group, item) => new
{
TrackedEntity = item
});
var condition = shrinkGroup.Where(x =>
(x.TrackedEntity.Logs == null || !x.TrackedEntity.Logs.Any())
|| (x.TrackedEntity.Logs.Any() && x.TrackedEntity.Logs.OrderBy(c => c.DateTime).FirstOrDefault().Type != TrackTypeEnum.Delete));
var catchEntity = condition.Select(x => x.TrackedEntity.Entity);
return catchEntity;
}
某些行可能没有任何日志状态。
我将获得“未删除”用户:
var models = _users.Filtered(_logs);
var foundUser = await (from user in models
where user.Id == currentUser.Id
where user.Username == currentUser.Username
where user.Mobile == currentUser.Mobile
where user.Role == currentUser.Role
where user.Password == currentUser.EncryptedPassword
where user.FirstName == currentUser.FirstName
where user.LastName == currentUser.LastName
where user.Phone == currentUser.Phone
select user).FirstOrDefaultAsync().ConfigureAwait(false);
return foundUser != null;
但是,当我想获取不被删除的用户(基于Log
的{{1}}属性)时,它将返回此错误:
Type
请帮助我解决该问题,并根据InvalidCastException: Unable to cast object of type 'DefaultIfEmptyAsyncIterator`1[RealEstate.Domain.Tables.Log]' to type 'System.Collections.Generic.IEnumerable`1[RealEstate.Domain.Tables.Log]'.
状态获取未删除的特定行。
谢谢。