我在我的复杂 lambda 查询中使用 Distinct 但不起作用
I use Distinct in my complex lambda query but is not work
我想从没有任何关系的两个Table中搜索
这些是我的桌子
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"
"newsText_1" "newsText_2"
"subFolderText_1" "subFolderText_2"
对于预期的结果,您可以更改您的视图,如下所示:
<table class="table">
<thead>
<tr>
<th></th>
</tr>
</thead>
<tbody>
<tr>
@foreach (var item in Model.SearchViewModels.Select(s => new { s.NewsTitle }).Distinct())
{
<td>
<a href="#">@item.NewsTitle</a>
</td>
}
</tr>
<tr>
@foreach (var item in Model.SearchViewModels.Select(s => new { s.SubFolderTitle }).Distinct())
{
<td>
<a href="#">@item.SubFolderTitle</a>
</td>
}
</tr>
</tbody>
</table>
我想从没有任何关系的两个Table中搜索 这些是我的桌子
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"
"newsText_1" "newsText_2"
"subFolderText_1" "subFolderText_2"
对于预期的结果,您可以更改您的视图,如下所示:
<table class="table">
<thead>
<tr>
<th></th>
</tr>
</thead>
<tbody>
<tr>
@foreach (var item in Model.SearchViewModels.Select(s => new { s.NewsTitle }).Distinct())
{
<td>
<a href="#">@item.NewsTitle</a>
</td>
}
</tr>
<tr>
@foreach (var item in Model.SearchViewModels.Select(s => new { s.SubFolderTitle }).Distinct())
{
<td>
<a href="#">@item.SubFolderTitle</a>
</td>
}
</tr>
</tbody>
</table>