I have a code table:
public class Code
{
[Key]
public int CodeID { get; set; }
[Required]
[StringLength(30)]
public string Title { get; set; }
[Required]
[StringLength(150)]
public string Description { get; set; }
public DateTime DateAdded { get; set; }
public DateTime LastUpdated { get; set; }
[Required]
[StringLength(30)]
public string Project { get; set; }
[Required]
[StringLength(30)]
public string CMS { get; set; }
public int DotNetVersion { get; set; }
[Required]
[StringLength(150)]
public string Dependencies { get; set; }
[StringLength(30)]
public string Author { get; set; }
public string CodeFile { get; set; }
[Required]
[StringLength(100)]
public string TFSLocation { get; set; }
////Creates a relationship in the DB with Tag
//[ForeignKey("TagID")]
public virtual ICollection<Tag> Tags { get; set; }
////Purely for API
//[Required]
public int TagID { get; set; }
}
A tag table:
public class Tag
{
[Key]
public int TagID { get; set; }
[Required]
[StringLength(30)]
public string TagName { get; set; }
////Creates a relationship in the DB with Code
public virtual ICollection<Code> Code { get; set; }
}
And a view model:
public class CodeTagViewModel
{
public List<Tag> Tags { get; set; }
public List<Tag> SelectedTags { get; set; }
public int CodeID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public DateTime DateAdded { get; set; }
public DateTime LastUpdated { get; set; }
public string Project { get; set; }
public string CMS { get; set; }
public int DotNetVersion { get; set; }
public string Dependencies { get; set; }
public string Author { get; set; }
public string CodeFile { get; set; }
public string TFSLocation { get; set; }
}
And I’m trying to run a query to get code files that are linked to tag names that have been searched. At the moment I have something like this:
List<CodeTagViewModel> models = new List<CodeTagViewModel>();
List<Code> codes = db.Code.ToList<Code>();
foreach (Code code in codes)
{
models.Add(MapCodeToModel(code));
}
var orderedModels = models.ToList();
if (!String.IsNullOrEmpty(searchString))
{
orderedModels = models.Where(x => x.Title.ToUpper().Contains(searchString.ToUpper()) || x.Description.ToUpper().Contains(searchString.ToUpper()) || x.Project.ToUpper().Contains(searchString.ToUpper()) || x.CMS.ToUpper().Contains(searchString.ToUpper()) || x.Dependencies.ToUpper().Contains(searchString.ToUpper()) || x.Author.ToUpper().Contains(searchString.ToUpper())).ToList();
if(orderedModels.Count == 0)
{
var Tags = db.Tags;
orderedModels = models.SelectMany(x => x.SelectedTags).Select(t => t).Where(t => t.TagName).Contains(searchString).ToList();
}
}
return View(orderedModels);
Searching based on the other columns of the code table work fine, I just included them so you could get a better idea of what I am trying to do; maybe there is a better way than doing my if statement to see if the search has matched anything else first. It’s just searching on Tags that doesn’t seem to be working for me.
The part I need help with:
orderedModels = models.SelectMany(x => x.SelectedTags).Select(t => t).Where(t => t.TagName).Contains(searchString).ToList();
Solved it:
MapCodeToModel is my own self explanatory method that I can post on request if it will help.