int'不包含'Contains'的定义,并且最好的扩展方法重载'Queryable.Contains(IQueryable,string)'需要类型为'IQueryable 的接收器
这是Controller代码的一部分:
var projects = db.Projects.Include(p => p.Engineer).Include(p => p.SiteLocation);
// var projects = from p in db.Projects
// select p;
if (!String.IsNullOrEmpty(searchString))
{
projects = projects.Where(p => p.NumberMCP.Contains(searchString)
|| p.nameProject.Contains(searchString) ||
p.Ptype.Contains(searchString) || p.EngineerID.Contains(searchString));
}
项目模型
public partial class Project
{
public int ProjectID { get; set; }
[DisplayName("MCP")]
[Required(ErrorMessage = "NumberMCP es requerido")]
public string NumberMCP { get; set; }
[DisplayName("Ingeniero")]
[Required(ErrorMessage = "Tarjeta Ingeniero es requerido")]
public int EngineerID { get; set; }
[DisplayName("Localidad")]
[Required(ErrorMessage = "ID Localidad es requerido")]
public int SiteLocationID { get; set; }
[DisplayName("Descripción")]
[Required(ErrorMessage = "Descripción es requerido")]
public string nameProject { get; set; }
[DisplayName("Tipo Proyecto")]
[Required(ErrorMessage = "Tipo Proyecto es requerido")]
public string Ptype { get; set; }
[DisplayName("Año")]
[Required(ErrorMessage = "Año es requerido")]
public Nullable<int> Pyear { get; set; }
[DisplayName("Link del Proyecto")]
[Required(ErrorMessage = "Link del Proyecto")]
public string Plink { get; set; }
public virtual Engineer Engineer { get; set; }
public virtual SiteLocation SiteLocation { get; set; }
}
}
答案 0 :(得分:1)
使用int.TryParse
并按字面比较整数:
if (!String.IsNullOrEmpty(searchString))
{
int searchInt = 0;
var isInt = int.TryParse(searchString, out searchInt);
projects = projects.Where(p =>
(isInt && p.NumberMCP == searchInt) ||
(!isInt && p.nameProject.Contains(searchString)) ||
(!isInt && p.Ptype.Contains(searchString)) ||
(isInt && p.EngineerID == searchInt)
);
}