Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

c# - IQueryable convert int to string - sortOrder and Filtring

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; }
}

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)
    );
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...