Ale*_*lex 5 .net c# linq dynamicquery entity-framework-6
我正在为我的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; }
}
Run Code Online (Sandbox Code Playgroud)
我想搜索,即在申请人类型学院的教育中提交的机构名称.申请人可以拥有一个或多个ApplicantEducations对象.
以下是我的申请人教育的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; }
}
Run Code Online (Sandbox Code Playgroud)
我的学院实体课就是这样的
public class Institute
{
public int Id { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
因此,用户将通过指定学院名称进行搜索,所有申请人将通过该学院的教育进行检索.
正如我上面提到的链接.下面举例说明字符串字段谓词构建
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);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码用于构建主实体类(申请人)中包含的简单字符串字段的谓词.但申请人也有ApplicantEducation集合,所以我的问题是如何为where子句(方法)创建动态查询(谓词),linq这样当用户搜索学院名称时,所有申请人都将获得相同的教育.
我的搜索条件如下,
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; }
}
Run Code Online (Sandbox Code Playgroud)
我有点失落,这怎么可能.
谢谢
| 归档时间: |
|
| 查看次数: |
1349 次 |
| 最近记录: |