请帮我在C#中将此SQL语句转换为Linq:
string cmd = "SELECT * FROM dbo.Personnel WHERE (dbo.Personnel.FName LIKE
@FName + '%' AND dbo.Personnel.LName LIKE @LName +'%')";
if (objPPersonnel.PersonnelCode != 0)
{
cmd += "AND ( dbo.Personnel.PersonnelCode =@PersonnelCode ) ";
}
if (objPPersonnel.NationalCode != "0")
{
cmd += "AND ( dbo.Personnel.NationalCode =@NationalCode ) ";
}
if (objPPersonnel.OrganizationPostId > 0)
{
cmd += "AND ( dbo.Personnel.OrganizationPostId =@OrganizationPostId ) ";
}
答案 0 :(得分:0)
我不确定这是否是您要求的:
var query = from c in dbContext.Personnel
where c.Personnel.LName.Contains(parLName)
$$ ((c.Personnel.PersonnelCode = parPersonnelCode && parPersonnelCode != 0)||parPersonnelCode == 0)
$$ ((c.Personnel.NationalCode = parNationalCode && parNationalCode != 0)||parNationalCode == 0)
$$ ((c.Personnel.OrganitationPostId = parOrganitationPostId && parOrganitationPostId > 0)||parOrganitationPostId == 0)
select c;
答案 1 :(得分:0)
persons.Where(p => p.FirstName.StartsWith("firstName") &&
p.LastName.StartsWith("lastName") &&
(p.PersonCode != 0 ? p.personCode == personCode : true) ...
答案 2 :(得分:0)
这样做:
var result = Personnel
.Where(p =>
{
p.FName.IndexOf(firstName) == 0
&& p.LName.IndexOf(lastName) == 0
&&
(
(p.PersonnelCode == personnelCode && objPPersonnel.PersonnelCode != 0) ||
(p.NationalCode == nationalCode && objPPersonnel.NationalCode != 0) ||
(p.OrganizationPostId == organizationPostId && objPPersonnel.OrganizationPostId != 0)
)
});