问题
int' does not contain a definition for 'Contains' and the best extension method overload 'Queryable.Contains(IQueryable, string)' requires a receiver of type 'IQueryable
This is part of the code of the 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));
}
Projects Model
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; }
}
}
回答1:
Use int.TryParse
and literally compare ints to ints:
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)
);
}
来源:https://stackoverflow.com/questions/44416992/iqueryable-convert-int-to-string-sortorder-and-filtring