【发布时间】:2018-09-22 03:15:25
【问题描述】:
我正在为我的 MVC EF 应用程序创建搜索功能。我正在使用动态查询创建它。并按照这个方法https://www.codeproject.com/Articles/493917/Dynamic-Querying-with-LINQ-to-Entities-and-Express
它用于为实体的bool 和string 字段创建谓词。我的应用程序中的主要实体是Applicant
EDMX Applicant 正在关注
public partial class Applicant
{
public Applicant()
{
this.ApplicantEducations = new HashSet<ApplicantEducation>();
this.ApplicantSkills = new HashSet<ApplicantSkill>();
this.Applications = new HashSet<Application>();
this.Experiences = new HashSet<Experience>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public byte[] CV_Upload1 { get; set; }
public string CV_Upload2 { get; set; }
public string email { get; set; }
public string password { get; set; }
public Nullable<System.DateTime> DOB { get; set; }
virtual ICollection<ApplicantEducation> ApplicantEducations { get; set; }
virtual ICollection<ApplicantSkill> ApplicantSkills { get; set; }
virtual ICollection<Application> Applications { get; set; }
virtual ICollection<Experience> Experiences { get; set; }
}
我想搜索,即在类型为 Institute 的申请人教育中归档的机构名称。申请人可以有一个或多个申请人教育对象。
以下是我的申请者教育的 EDMX 课程
public partial class ApplicantEducation
{
public int id { get; set; }
public Nullable<int> ApplicantId { get; set; }
public Nullable<int> InstituteId { get; set; }
public Nullable<int> EducationLevelId { get; set; }
public Nullable<bool> IsComplete { get; set; }
public Nullable<System.DateTime> DateStart { get; set; }
public Nullable<System.DateTime> DateEnd { get; set; }
public Nullable<short> GPA { get; set; }
public virtual EducationLevel EducationLevel { get; set; }
public virtual Institute Institute { get; set; }
public virtual Applicant Applicant { get; set; }
}
而我的研究所实体类是这样的
public class Institute
{
public int Id { get; set; }
public string Name { get; set; }
}
因此,用户将通过指定机构名称进行搜索,所有申请人都将从该机构获得教育。
正如我上面提到的链接。下面以字符串字段谓词构建为例进行演示
private static Expression<Func<TDbType, bool>> ApplyStringCriterion<TDbType,
TSearchCriteria>(TSearchCriteria searchCriteria, PropertyInfo searchCriterionPropertyInfo,
Type dbType, MemberInfo dbFieldMemberInfo, Expression<Func<TDbType, bool>> predicate)
{
// Check if a search criterion was provided
var searchString = searchCriterionPropertyInfo.GetValue(searchCriteria) as string;
if (string.IsNullOrWhiteSpace(searchString))
{
return predicate;
}
// Then "and" it to the predicate.
// e.g. predicate = predicate.And(x => x.firstName.Contains(searchCriterion.FirstName)); ...
// Create an "x" as TDbType
var dbTypeParameter = Expression.Parameter(dbType, @"x");
// Get at x.firstName
var dbFieldMember = Expression.MakeMemberAccess(dbTypeParameter, dbFieldMemberInfo);
// Create the criterion as a constant
var criterionConstant = new Expression[] { Expression.Constant(searchString) };
// Create the MethodCallExpression like x.firstName.Contains(criterion)
var containsCall = Expression.Call(dbFieldMember, StringContainsMethod, criterionConstant);
// Create a lambda like x => x.firstName.Contains(criterion)
var lambda = Expression.Lambda(containsCall, dbTypeParameter) as Expression<Func<TDbType, bool>>;
// Apply!
return predicate.And(lambda);
}
上面的代码用于为包含在主实体类 (Applicant) 中的简单字符串字段构建谓词。但是申请人也有申请人教育集合,所以我的问题是如何为linq的where子句(方法)创建一个动态查询(谓词),这样当用户搜索机构名称时,所有申请人都会以相同的教育被检索。
我的搜索条件如下,
public class SearchCriteriaVM
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime? DOB { get; set; }
public string Description { get; set; }
public ICollection<Models.ApplicantEducationVM> ApplicantEducations { get; set; }
public ICollection<Models.ExperienceVM> Experiences { get; set; }
public ICollection<Models.ApplicantSkillsVM> ApplicantSkills { get; set; }
public ICollection<Models.ApplicationsVM> Applications { get; set; }
}
我有点迷茫,这怎么可能。
谢谢
【问题讨论】:
-
有可能(必须生成类似
outer.Collection.Any(inner_predicate)的内容),但不确定您将如何指定标准,因为您所遵循的示例是从一个简单的平面对象中获取标准信息。跨度> -
@IvanStoev 我已经用标准更新了我的问题
-
你已经在这里回答了这个问题:请参考这个链接stackoverflow.com/questions/25508595/…
-
@SwetaNair 好的,但是如果你在这里回答并解释,那么我可以接受。
标签: c# .net linq entity-framework-6 dynamicquery