IQueryable convert int to string - sortOrder and Filtring

社会主义新天地 提交于 2019-12-25 09:43:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!