私はコードテーブルを持っています:
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();