我想将连接语句结果设置为模型视图。
但问题是:要搜索4个名称字段,但只有第一个名称是强制性的,而另一个名称不是。
LINQ语句:
public static List<Laborer> getLaborerByName(int? laborOfficeNo, long? componyNo, string firstName, string secondName, string thirdName, string fourthName)
{
var queryGetLaborerByName = (from c in db.Compny
join x in db.Laborer on c.PK_componyId equals x.FK_componyId
where c.SequenceNumber == componyNo
&& c.FK_LaborOfficeId == laborOfficeNo
select x);
if (!String.IsNullOrEmpty(firstName))
queryGetLaborerByName = queryGetLaborerByName.Where(pr => pr.FirstName.Contains(firstName));
if (!String.IsNullOrEmpty(secondName))
queryGetLaborerByName = queryGetLaborerByName.Where(pr => pr.SecondName.Contains(secondName));
if (!String.IsNullOrEmpty(thirdName))
queryGetLaborerByName = queryGetLaborerByName.Where(pr => pr.ThirdName.Contains(thirdName));
if (!String.IsNullOrEmpty(thirdName))
queryGetLaborerByName = queryGetLaborerByName.Where(pr => pr.FourthName.Contains(fourthName));
if (queryGetLaborerByName.ToList().Count > 0)
{
return queryGetLaborerByName.ToList();
}
return null;
}
ViewModle:
public string LaborerFirstName { get; set; }
public string LaborerSecondName { get; set; }
public string LaborerThirdName { get; set; }
public string LaborerFourthName { get; set; }
如何使其在模型中工作并保持serach过滤。
我希望明白
似乎不可理解。 我想要的是保持搜索过滤。接受这些情景:
仅按名字搜索。结果:所有员工都将输入姓名的第一个名字。
按名字和第二名称搜索。结果:将所有员工姓名都输入名字和名字第二名。
按名字和第二名称搜索第三名。结果:所有员工姓名都有名字和第二名,第三名与姓名相符。
按名字和第二名称搜索第三名和第四名。结果:所有员工姓名都有第一名和第二名,第三名和第四名匹配输入的姓名。所以这里大多数都会显示所需员工的姓名。
答案 0 :(得分:0)
注意:这不是一个尝试作为答案,但我真的不得不提到这一点。
您的代码目前有
if (queryGetLaborerByName.ToList().Count > 0)
{
return queryGetLaborerByName.ToList();
}
我强烈建议您使用
结束方法 return queryGetLaborerByName.ToList();
让被叫者处理集合中没有项目的事实。拨打.ToList()
两次,再拨打Count()
(使用Any()
作为替代方法,查看列表是否包含任何项目。Count()
将遍历每个项目,同时一旦遇到第一个项目,Any()
就会停止。)不是最佳做法。
答案 1 :(得分:0)
您需要做的就是使用允许匹配或空字符串的条件或条件:
(SecondName equals SecondName or SecondName equals EmptyString) and
(ThirdName equals ThirdName or ThirdName equals EmptyString) and
(ForthdName equals ForthName or ForthdName equals EmptyString) and
实际上,如果SecondName,ThirdName,ForthName不为空且不匹配,则该行将不会返回,因为它不匹配且不为空。
public class One
{
public static List<Laborer> getLaborerByName(int? laborOfficeNo, long? componyNo, string firstName, string secondName, string thirdName, string fourthName)
{
var queryGetLaborerByName = (from c in db.Compny
join x in db.Laborer on c.PK_componyId equals x.FK_componyId
where c.SequenceNumber == componyNo
&& c.FK_LaborOfficeId == laborOfficeNo
&& c.FirstName.Equals(firstName) // Force First Name match
&& (c.SecondName.Equals(secondName) || String.IsNullOrEmpty(c.SecondName) // Accepts SecondName Match or SecondName is EmptyString
&& (c.ThirdName.Equals(thirdName) || String.IsNullOrEmpty(c.ThirdName) // Accepts ThirdName Match or ThirdName is EmptyString
&& (c.ForthNameName.Equals(forthName) || String.IsNullOrEmpty(c.ForthName) // Accepts ForthName Match or ForthName is EmptyString
select x);
return queryGetLaborerByName;
}
}
<强>修强>
public class One
{
public static List<Laborer> getLaborerByName(int? laborOfficeNo, long? componyNo, string firstName, string secondName, string thirdName, string fourthName)
{
var queryGetLaborerByName = (from c in db.Compny
join x in db.Laborer on c.PK_componyId equals x.FK_componyId
where c.SequenceNumber == componyNo
&& c.FK_LaborOfficeId == laborOfficeNo
&& c.FirstName.Equals(firstName) // Force First Name match
&& (c.SecondName.Equals(secondName) || String.IsNullOrEmpty(secondName) // Accepts SecondName Match or SecondName is EmptyString
&& (c.ThirdName.Equals(thirdName) || String.IsNullOrEmpty(thirdName) // Accepts ThirdName Match or ThirdName is EmptyString
&& (c.ForthNameName.Equals(forthName) || String.IsNullOrEmpty(forthName) // Accepts ForthName Match or ForthName is EmptyString
select x);
return queryGetLaborerByName;
}
}