我正试图开发一个可过滤的主题列表。有2个用外键连接的表,我应该写一个LINQ查询,通过客户端发送的 "SpecId == 2 AND ValueBit == true "这样的字符串过滤来搜索主题。我应该写一个LINQ查询,通过客户端发送的字符串过滤器来搜索主题,比如 "SpecId == 2 AND ValueBit == true"。我在我的服务项目中使用了存储库模式,并试图这样做,但我无法实现我想要的过滤器。
我花了一整天的时间,都没能解决这个查询。请你帮帮我,好吗?
我的查询
Topics = await topicRepository.GetAllWithStringOrder(x => new TopicModel
{
CreatedDate = x.CreatedDate,
Subtitle = x.Subtitle,
Title = x.Title,
UpdatedDate = x.UpdatedDate,
Users = x.User.UserProfiles.Select(y => new UserProfileModel
{
Nickname = y.Nickname
}).ToList(),
TopicId = x.TopicId,
UserId = x.UserId,
TopicCommentCount = x.TopicComments.Count,
TopicSpecs = x.TopicSpecs.Where(y => y.Spec.ShowInTopic).Select(y => new TopicSpecModel
{
ValueBit = y.ValueBit,
ValueDate = y.ValueDate,
ValueNumber = y.ValueNumber,
ValueString = y.ValueString,
Spec = new SpecModel
{
Name = y.Spec.Name,
SpecId = y.Spec.SpecId,
Color = y.Spec.Color
}
}).ToList()
}, x => x.IsActive && !x.IsDeleted && x.IsLatest && x.TopicSpecs.Where(condition).Count() > 0, model.ItemsPerPage.Value, model.PageNumber.Value, model.SortBy, !model.Reverse)
GetAllWithStringOrder
public virtual async Task<List<TReturn>> GetAllWithStringOrder<TReturn>(Expression<Func<TEntity, TReturn>> selectExp, Expression<Func<TEntity, bool>> whereExp, int itemsPerPage, int pageNumber, string orderBy, bool isDescending, params Expression<Func<TEntity, object>>[] includeExps)
{
var query = DbSet.Where(whereExp);
if (!string.IsNullOrEmpty(orderBy))
query = query.OrderBy(orderBy);
if (includeExps != null)
query = includeExps.Aggregate(query, (current, exp) => current.Include(exp));
query = query.OrderBy(orderBy, isDescending).Skip<TEntity>((pageNumber - 1) * itemsPerPage).Take<TEntity>(itemsPerPage);
return await query.Select(selectExp).ToListAsync();
}
主题表
public partial class Topic
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Topic()
{
this.TopicComments = new HashSet<TopicComment>();
this.TopicContents = new HashSet<TopicContent>();
this.TopicSpecs = new HashSet<TopicSpec>();
}
public int TopicId { get; set; }
public string Title { get; set; }
public string Subtitle { get; set; }
public int UserId { get; set; }
public System.DateTimeOffset CreatedDate { get; set; }
public Nullable<System.DateTimeOffset> UpdatedDate { get; set; }
public bool IsDeleted { get; set; }
public bool IsLatest { get; set; }
public bool IsActive { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<TopicComment> TopicComments { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<TopicContent> TopicContents { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<TopicSpec> TopicSpecs { get; set; }
public virtual User User { get; set; }
}
主题规格表
public partial class TopicSpec
{
public int TopicSpecId { get; set; }
public int TopicId { get; set; }
public int SpecId { get; set; }
public string ValueString { get; set; }
public Nullable<bool> ValueBit { get; set; }
public Nullable<System.DateTimeOffset> ValueDate { get; set; }
public Nullable<int> ValueNumber { get; set; }
public bool IsActive { get; set; }
public virtual Spec Spec { get; set; }
public virtual Topic Topic { get; set; }
}
规格表
public partial class Spec
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Spec()
{
this.TopicSpecs = new HashSet<TopicSpec>();
}
public int SpecId { get; set; }
public int SpecTypeId { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
public bool ShowInTopic { get; set; }
public string Color { get; set; }
public virtual SpecType SpecType { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<TopicSpec> TopicSpecs { get; set; }
}
你在某个地方有一个Model类,这个查询是从该查询的最后一行中提取属性来运行的。
..... model.ItemsPerPage.Value, model.PageNumber.Value, ....
如果能把这个类也公布出来可能会有帮助 尽管这已经有很多代码需要筛选了。
也就是说,问题#1的最简单的解决方案可能是在该类中添加一些属性。model
类,并在 Where
子句。
... x.TopicSpecs.Where(y=> y.SpecId == model.SpecIdValue && y.ValueBit == model.ValueBitFlag)....
在这个例子中,新的模型属性是 model.SpecIdValue
作为 int
和 model.ValueBitFlag
作为 bool
.
我想你有那个电话 Topics = await topicRepository.GetAllWithStringOrder...
的内部,所以你可以设置我上面提到的模型属性,然后再次调用那个方法以获得更新的响应。
对于问题#2,如果你正在寻找一个类型为 Topic
作为查询的返回,你应该使用topicRepository。