我在复杂的lambda查询中使用了Distinct,但无法正常工作

我想从没有任何关系的两个表中搜索 这些是我的桌子

     public class News
     {
        public int Id { get; set; }
        public string Title { get; set; }
        public string FullText { get; set; }
     }
     public class SubFolder
     {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Text { get; set; }
     }

为此,我创建一个ViewModel并实现IEquatable <>

 public class SearchViewModels:IEquatable<SearchViewModels>
{
    public int NewsId { get; set; }

    public string NewsText { get; set; }
    public string NewsTitle { get; set; }
    public int SubId { get; set; }
    public string SubFolderText { get; set; }
    public string SubFolderTitle { get; set; }

    public bool Equals([AllowNull] SearchViewModels other)
    {
          //Check whether the compared object is null. 
    if (Object.ReferenceEquals(other, null)) return false;

    //Check whether the compared object references the same data. 
    if (Object.ReferenceEquals(this, other)) return true;

        return NewsTitle.Equals(other.NewsTitle) && 
               NewsId.Equals(other.NewsId)&&
               NewsText.Equals(other.NewsText)&&
               SubFolderTitle.Equals(other.SubFolderTitle)&&
               SubFolderText.Equals(other.SubFolderText)&&
               SubId.Equals(other.SubId); 
    }
    public override int GetHashCode()
    {
        int hashNewsText = NewsText == null ? 0: NewsText.GetHashCode();
        int hashNewsID = NewsId == null ? 0 :NewsId.GetHashCode();
        int hashNewsTitle = NewsTitle == null ? 0 :NewsTitle.GetHashCode();

        int hashSubTitle = SubFolderTitle == null ? 0 :SubFolderTitle.GetHashCode();
        int hashSubText = SubFolderText == null ? 0 :SubFolderText.GetHashCode();
        int hashSubId = SubId == null ? 0 :SubId.GetHashCode();

        return hashNewsText ^ hashNewsID ^ hashNewsTitle ^ hashSubId ^ hashSubText ^ hashSubTitle;
    }
}

我的lambda搜索查询在剃刀页面中:

 [BindProperty]
        public  IEnumerable<Model.SearchViewModels> SearchViewModels { get; set; }
public void OnGet(string searchParam)
    {

     SearchViewModels = _context.News.SelectMany(a => _context.SubFolders, (a, b) => new {a,b})
       .Where (x=>x.a.FullText.Contains( searchParam) && x.a.Active && x.b.Text.Contains( searchParam) && x.b.Publish)


       .Select(x =>new  SearchViewModels{ 
          NewsText = x.a.FullText,NewsId = x.a.Id,NewsTitle = x.a.Title, SubFolderText= x.b.Text,SubId =x.b.Id, SubFolderTitle= x.b.Title})
           .Distinct().ToList();

       }
}

鉴于这样写

@foreach (var item in Model.SearchViewModels)
    {

    <div class="btn btn-primary" >@nne</div> 
        @* @item.NewsId *@ <br>
        <a href="#">@item.NewsTitle</a>
       @* @Html.Raw(item.NewsText) *@
         @* @item.SubId *@<br>
        <a href="#">@item.SubFolderTitle</a>
       @* @Html.Raw(item.SubFolderText) *@
       <hr>
       nne++;
    }

我得到数据但是像这样

“ newsText_1”   “ subFolderText_1”      “ newsText_1”   “ subFolderText_2”      “ newsText_2”   “ subFolderText_1”      “ newsText_2”   “ subFolderText_1”

我怎么能这样

“ newsText_1”   “ newsText_2”      “ subFolderText_1”   “ subFolderText_2”
评论