我有一个代码表:
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; }
}
一个标签表:
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; }
}
和一个视图模型:
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; }
}
我正在尝试运行查询以获取链接到已搜索标签名称的代码文件。目前我有这样的事情:
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);
根据代码表的其他列进行搜索工作正常,我只是将它们包括在内,这样您就可以更好地了解我想要做什么;也许有比执行我的 if 语句更好的方法来查看搜索是否首先匹配其他任何内容。它只是搜索似乎对我不起作用的标签。
我需要帮助的部分:
orderedModels = models.SelectMany(x => x.SelectedTags).Select(t => t).Where(t => t.TagName).Contains(searchString).ToList();