SQL 서버 union
에서 결과는 기본 키 열을 기준으로 정렬됩니다. 엔티티 프레임 워크에서이 동작을 방지하고 싶습니다.
this post에서 @praveen은 pure SQL에서이 작업을 수행하는 방법을 설명했습니다. 하지만 엔티티 프레임 워크에서이 작업을 수행하려고합니다.
내 코드 :엔터티 프레임 워크에서 유니온 정렬 결과 방지
public virtual ActionResult Search(string keyword)
{
var products = _db.Products
.Where(x => x.IsActive)
.AsQueryable();
var productExactlyTitle = products.Where(x => x.Title == keyword);
var productStartTitle = products.Where(x => x.Title.StartsWith(keyword));
var productContainsTitle = products.Where(x => x.Title.Contains(keyword)
|| x.Title.Contains(keyword)
|| x.SubTitle.Contains(keyword)
|| x.OtherName.Contains(keyword));
var productList = productExactlyTitle.Union(productStartTitle)
.Union(productContainsTitle)
.Take(10)
.AsEnumerable()
.Select(x => new ProductItemViewModel()
{
Id = x.Id,
Title = x.Title,
Price = x.Price.ToPrice(),
Image = x.Images.FirstOrDefault(y => y.IsCoverPhoto)?.ImageUrl
});
// some code ...
}
나는 순서 아래에있는 레코드를 표시하려면 :
첫째 : 의 기록 productExactlyTitle
둘째 : 의 기록 productStartTitle을
셋째 : 의 기록 productContainsTitle
결과는 Id
열로 정렬됩니다! 나는 이것을 원하지 않는다.
방법이 있나요?
당신은 우리에게 당신이 시도, 그렇지 않으면 우리가 도울 수있는 코드를 표시해야합니다. https://stackoverflow.com/help/how-to-ask –