我是新来的,所以请宽容:)
我的查询如下:
ThesisListViewModel reviewModel = new ThesisListViewModel
{
ThesisModel = from the in thesisNavigatorRepository.Thesis
join tp1 in thesisNavigatorRepository.ThesisType1 on the.ThesisType1Id equals tp1.ThesisType1Id
join tp2 in thesisNavigatorRepository.ThesisType2 on the.ThesisType2Id equals tp2.ThesisType2Id
select new ThesisModel
{
Thesis_ThesisId = the.ThesisId,
Thesis_Subject = the.Subject,
Thesis_ShortDescription = the.ShortDescription,
ThesisType1_Description = tp1.Description,
ThesisType2_Description = tp2.Description,
Thesis_URL = the.URL,
Thesis_ThesisLocalization = the.ThesisLocalization,
AuthorModel = from per in thesisNavigatorRepository.Person
join uod in thesisNavigatorRepository.UnitOfDepartment on per.UODId equals uod.UODId
join dep in thesisNavigatorRepository.Department on uod.DepartmentId equals dep.DepartmentId
join ptt in thesisNavigatorRepository.PersonToThesis on per.PersonId equals ptt.PersonId
join prr in thesisNavigatorRepository.PersonRole on ptt.PersonRoleId equals prr.PersonRoleId
where ptt.ThesisId == the.ThesisId && ptt.PersonRoleId == 1
select new AuthorModel
{
Person_FistName = per.FirstName,
Person_LastName = per.LastName,
UnitOfDepartment_ShortName = uod.ShortName,
UnitOfDepartment_LongName = uod.LongName,
Department_ShortName = dep.ShortName,
Department_LongName = dep.LongName,
},
StaffModel = from per in thesisNavigatorRepository.Person
join uod in thesisNavigatorRepository.UnitOfDepartment on per.UODId equals uod.UODId
join dep in thesisNavigatorRepository.Department on uod.DepartmentId equals dep.DepartmentId
join ptt in thesisNavigatorRepository.PersonToThesis on per.PersonId equals ptt.PersonId
join prr in thesisNavigatorRepository.PersonRole on ptt.PersonRoleId equals prr.PersonRoleId
where ptt.ThesisId == the.ThesisId && ptt.PersonRoleId != 1
select new StaffModel
{
Person_FistName = per.FirstName,
Person_LastName = per.LastName,
Person_Title = per.Title,
UnitOfDepartment_ShortName = uod.ShortName,
UnitOfDepartment_LongName = uod.LongName,
Department_ShortName = dep.ShortName,
Department_LongName = dep.LongName,
},
},
ThesisType1Model = thesisNavigatorRepository.ThesisType1,
ThesisType2Model = thesisNavigatorRepository.ThesisType2,
PersonModel = thesisNavigatorRepository.Person,
PersonRoleModel = thesisNavigatorRepository.PersonRole,
UnitOfDepartmentModel = thesisNavigatorRepository.UnitOfDepartment,
DepartmentModel = thesisNavigatorRepository.Department
};
return PartialView(reviewModel);
我想让它依赖于我传递给action方法的一些属性。
我需要的条件如下(简单版):
if(property1 == null or property2 == null) -> query for ThesisModel without where clause
elseif(property1 == null) ThesisModel where tp2.Description like property2
elseif(property2 == null) ThesisModel where tp1.Description like property1
else ThesisModel where tp1.Description like property1 and tp2.Description like property2
我被困住了。我当然可以针对每个条件进行不同的查询,但我希望这是最简单的方法。
你能帮忙吗?
P.S。 我的英语很差,如果我犯了一些错误就很抱歉。
编辑:
我尝试过你的想法,但我不能在我的项目中使用它。 我使用以下属性从视图传递数据到我的控制器:
public string SearchByType1 { get; set; }
public string SearchByType2 { get; set; }
public string SearchByPerson { get; set; }
public string SearchByPersonRole { get; set; }
public string SearchByUOD { get; set; }
public string SearchByDepartment { get; set; }
您可以在我的控制器中看到每个属性与其他表相关。仅当我的条件与一个表中的一个属性相关时,才使用PredicateBuilder。目前有两个属性,我有这样的条件:
where
(
string.IsNullOrEmpty(model.SearchByType1)
&&
string.IsNullOrEmpty(model.SearchByType2)
)
||
(
string.IsNullOrEmpty(model.SearchByType1)
&&
SqlFunctions.StringConvert((double)tp2.ThesisType2Id).Trim().Equals(model.SearchByType2)
)
||
(
string.IsNullOrEmpty(model.SearchByType2)
&&
SqlFunctions.StringConvert((double)tp1.ThesisType1Id).Trim().Equals(model.SearchByType1)
)
||
(
SqlFunctions.StringConvert((double)tp1.ThesisType1Id).Trim().Equals(model.SearchByType1)
&&
SqlFunctions.StringConvert((double)tp2.ThesisType2Id).Trim().Equals(model.SearchByType2)
)
它有效,但看起来很糟糕,我没有进一步的想法。你能否详细说明你的想法和帮助?
仅供参考我使用SqlFunctions.StringConvert,因为参数来自(它们来自dropdownlist - id as string)。
答案 0 :(得分:0)
将每个条件声明为Func,然后将其传递给查询。有点像:
Func<string, bool> predicate;
if (prop1 == null && prop2 == null)
predicate = (s1, s2) => true;
else if (prop1 == null)
predicate = (s1, s2) => prop2 == s2;
else if (prop2 == null)
predicate = (s1, s2) => prop1 == s1;
else
predicate = (s1, s2) => prop1 == s1 && prop2 == s2;
var model = thesisNavigatorRepository.Thesis.Where(t => predicate(t.Description1, t.Description2));
答案 1 :(得分:0)
您可以使用PredicateBuilder
类
只需将Linqkit dll添加到您的项目中即可。 这是Linqkit链接:http://www.albahari.com/nutshell/predicatebuilder.aspx