Skip to content

Commit

Permalink
Merge pull request #918 from colinin/fix-sorting-qey
Browse files Browse the repository at this point in the history
fix(ef): 修复排序字段为空时的查询错误.
  • Loading branch information
colinin authored Dec 3, 2023
2 parents 06d5cfd + 0b6c815 commit cd23384
Show file tree
Hide file tree
Showing 24 changed files with 119 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
Expand Down Expand Up @@ -36,6 +37,10 @@ public async virtual Task<List<OrganizationUnit>> GetListAsync(
bool includeDetails = false,
CancellationToken cancellationToken = default)
{
if (sorting.IsNullOrWhiteSpace())
{
sorting = nameof(OrganizationUnit.Code);
}
return await (await GetDbSetAsync())
.IncludeDetails(includeDetails)
.Where(specification.ToExpression())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ public async virtual Task<long> GetCountAsync(string subjectId = null, string fi

public async virtual Task<List<PersistedGrant>> GetListAsync(string subjectId = null, string filter = null, string sorting = "CreationTime", int skipCount = 1, int maxResultCount = 10, CancellationToken cancellation = default)
{
if (sorting.IsNullOrWhiteSpace())
{
sorting = nameof(PersistedGrant.CreationTime);
}
return await (await GetDbSetAsync())
.WhereIf(!subjectId.IsNullOrWhiteSpace(), x => x.SubjectId.Equals(subjectId))
.WhereIf(!filter.IsNullOrWhiteSpace(), x =>
x.Type.Contains(filter) || x.ClientId.Contains(filter) || x.Key.Contains(filter))
.OrderBy(sorting ?? nameof(PersistedGrant.CreationTime))
.PageBy(skipCount, maxResultCount)
.ToListAsync(GetCancellationToken(cancellation));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,16 @@ protected async virtual Task<IQueryable<TextDifference>> BuildTextDifferenceQuer
string filter = null,
string sorting = nameof(TextDifference.Key))
{
if (sorting.IsNullOrWhiteSpace())
{
sorting = nameof(TextDifference.Key);
}

var textQuery = (await GetDbSetAsync())
.Where(x => x.CultureName.Equals(cultureName))
.WhereIf(!resourceName.IsNullOrWhiteSpace(), x => x.ResourceName.Equals(resourceName))
.WhereIf(!filter.IsNullOrWhiteSpace(), x => x.Key.Contains(filter))
.OrderBy(sorting ?? nameof(TextDifference.Key));
.OrderBy(sorting);

var targetTextQuery = (await GetDbSetAsync())
.Where(x => x.CultureName.Equals(targetCultureName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ public class GetLayoutListInput : PagedAndSortedResultRequestDto
{
public string Filter { get; set; }

public bool Reverse { get; set; }

[DynamicStringLength(typeof(LayoutConsts), nameof(LayoutConsts.MaxFrameworkLength))]
public string Framework { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ public class MenuGetListInput : PagedAndSortedResultRequestDto

public string Filter { get; set; }

public bool Reverse { get; set; }

public Guid? ParentId { get; set; }

public Guid? LayoutId { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public async virtual Task<MenuDto> GetAsync(Guid id)
public async virtual Task<ListResultDto<MenuDto>> GetAllAsync(MenuGetAllInput input)
{
var menus = await MenuRepository.GetAllAsync(
input.Filter, input.Sorting, input.Reverse,
input.Filter, input.Sorting,
input.Framework, input.ParentId, input.LayoutId);

return new ListResultDto<MenuDto>(
Expand All @@ -99,7 +99,7 @@ public async virtual Task<PagedResultDto<MenuDto>> GetListAsync(MenuGetListInput
var count = await MenuRepository.GetCountAsync(input.Filter, input.Framework, input.ParentId, input.LayoutId);

var menus = await MenuRepository.GetListAsync(
input.Filter, input.Sorting, input.Reverse,
input.Filter, input.Sorting,
input.Framework, input.ParentId, input.LayoutId,
input.SkipCount, input.MaxResultCount);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Task<int> GetCountAsync(

Task<List<Data>> GetPagedListAsync(
string filter = "",
string sotring = nameof(Data.Code),
string sorting = nameof(Data.Code),
bool includeDetails = false,
int skipCount = 0,
int maxResultCount = 10,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ Task<List<Layout>> GetPagedListAsync(
string framework = "",
string filter = "",
string sorting = nameof(Layout.Name),
bool reverse = false,
bool includeDetails = false,
int skipCount = 0,
int maxResultCount = 10,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ Task<int> GetCountAsync(
Task<List<Menu>> GetListAsync(
string filter = "",
string sorting = nameof(Menu.Code),
bool reverse = false,
string framework = "",
Guid? parentId = null,
Guid? layoutId = null,
Expand All @@ -106,7 +105,6 @@ Task<List<Menu>> GetListAsync(
Task<List<Menu>> GetAllAsync(
string filter = "",
string sorting = nameof(Menu.Code),
bool reverse = false,
string framework = "",
Guid? parentId = null,
Guid? layoutId = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace LINGYUN.Platform.Datas
{
Expand Down Expand Up @@ -56,21 +57,24 @@ public async virtual Task<int> GetCountAsync(

public async virtual Task<List<Data>> GetPagedListAsync(
string filter = "",
string sotring = "Code",
string sorting = "Code",
bool includeDetails = false,
int skipCount = 0,
int maxResultCount = 10,
CancellationToken cancellationToken = default)
{
sotring ??= nameof(Data.Code);
if (sorting.IsNullOrWhiteSpace())
{
sorting = nameof(Data.Code);
}

var dbSet = await GetDbSetAsync();
return await dbSet
.IncludeDetails(includeDetails)
.WhereIf(!filter.IsNullOrWhiteSpace(), x =>
x.Code.Contains(filter) || x.Description.Contains(filter) ||
x.DisplayName.Contains(filter) || x.Name.Contains(filter))
.OrderBy(sotring)
.OrderBy(sorting)
.PageBy(skipCount, maxResultCount)
.ToListAsync(GetCancellationToken(cancellationToken));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ public async virtual Task<List<Layout>> GetPagedListAsync(
string framework = "",
string filter = "",
string sorting = nameof(Layout.Name),
bool reverse = false,
bool includeDetails = false,
int skipCount = 0,
int maxResultCount = 10,
CancellationToken cancellationToken = default)
{
sorting ??= nameof(Layout.Name);
sorting = reverse ? sorting + " DESC" : sorting;
if (sorting.IsNullOrWhiteSpace())
{
sorting = nameof(Layout.Name);
}

return await (await GetDbSetAsync())
.IncludeDetails(includeDetails)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,15 @@ public async virtual Task<List<Menu>> GetAllChildrenWithParentCodeAsync(
public async virtual Task<List<Menu>> GetAllAsync(
string filter = "",
string sorting = nameof(Menu.Code),
bool reverse = false,
string framework = "",
Guid? parentId = null,
Guid? layoutId = null,
CancellationToken cancellationToken = default)
{
sorting ??= nameof(Menu.Code);
sorting = reverse ? sorting + " DESC" : sorting;
if (sorting.IsNullOrWhiteSpace())
{
sorting = nameof(Menu.Code);
}

return await (await GetDbSetAsync())
.WhereIf(parentId.HasValue, x => x.ParentId == parentId)
Expand All @@ -182,7 +183,7 @@ public async virtual Task<List<Menu>> GetAllAsync(
menu.Path.Contains(filter) || menu.Name.Contains(filter) ||
menu.DisplayName.Contains(filter) || menu.Description.Contains(filter) ||
menu.Redirect.Contains(filter))
.OrderBy(sorting)
.rting)

Check failure on line 186 in aspnet-core/modules/platform/LINGYUN.Platform.EntityFrameworkCore/LINGYUN/Platform/Menus/EfCoreMenuRepository.cs

View workflow job for this annotation

GitHub Actions / Build

; expected

Check failure on line 186 in aspnet-core/modules/platform/LINGYUN.Platform.EntityFrameworkCore/LINGYUN/Platform/Menus/EfCoreMenuRepository.cs

View workflow job for this annotation

GitHub Actions / Build

} expected

Check failure on line 186 in aspnet-core/modules/platform/LINGYUN.Platform.EntityFrameworkCore/LINGYUN/Platform/Menus/EfCoreMenuRepository.cs

View workflow job for this annotation

GitHub Actions / Build

; expected

Check failure on line 186 in aspnet-core/modules/platform/LINGYUN.Platform.EntityFrameworkCore/LINGYUN/Platform/Menus/EfCoreMenuRepository.cs

View workflow job for this annotation

GitHub Actions / Build

} expected
.ToListAsync(GetCancellationToken(cancellationToken));
}

Expand All @@ -207,16 +208,17 @@ public async virtual Task<int> GetCountAsync(
public async virtual Task<List<Menu>> GetListAsync(
string filter = "",
string sorting = nameof(Menu.Code),
bool reverse = false,
string framework = "",
Guid? parentId = null,
Guid? layoutId = null,
int skipCount = 0,
int maxResultCount = 10,
CancellationToken cancellationToken = default)
{
sorting ??= nameof(Menu.Code);
sorting = reverse ? sorting + " DESC" : sorting;
if (sorting.IsNullOrWhiteSpace())
{
sorting = nameof(Menu.Code);
}

return await (await GetDbSetAsync())
.WhereIf(parentId.HasValue, x => x.ParentId == parentId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ public async virtual Task<List<GroupMessage>> GetGroupMessagesAsync(
int maxResultCount = 10,
CancellationToken cancellationToken = default)
{
if (sorting.IsNullOrWhiteSpace())
{
sorting = nameof(GroupMessage.MessageId);
}
var groupMessages = await (await GetDbContextAsync()).Set<GroupMessage>()
.Distinct()
.Where(x => x.GroupId.Equals(groupId))
.Where(x => x.State == MessageState.Send || x.State == MessageState.Read)
.WhereIf(type.HasValue, x => x.Type.Equals(type))
.WhereIf(!filter.IsNullOrWhiteSpace(), x => x.Content.Contains(filter) || x.SendUserName.Contains(filter))
.OrderBy(sorting ?? nameof(GroupMessage.MessageId))
.OrderBy(sorting)
.PageBy(skipCount, maxResultCount)
.AsNoTracking()
.ToListAsync(GetCancellationToken(cancellationToken));
Expand Down Expand Up @@ -89,12 +93,16 @@ public async virtual Task<List<GroupMessage>> GetUserGroupMessagesAsync(
int maxResultCount = 10,
CancellationToken cancellationToken = default)
{
if (sorting.IsNullOrWhiteSpace())
{
sorting = nameof(GroupMessage.MessageId);
}
var groupMessages = await (await GetDbContextAsync()).Set<GroupMessage>()
.Distinct()
.Where(x => x.GroupId.Equals(groupId) && x.CreatorId.Equals(sendUserId))
.WhereIf(type != null, x => x.Type.Equals(type))
.WhereIf(!filter.IsNullOrWhiteSpace(), x => x.Content.Contains(filter) || x.SendUserName.Contains(filter))
.OrderBy(sorting ?? nameof(GroupMessage.MessageId))
.OrderBy(sorting)
.PageBy(skipCount, maxResultCount)
.AsNoTracking()
.ToListAsync(GetCancellationToken(cancellationToken));
Expand Down Expand Up @@ -162,7 +170,10 @@ public async virtual Task<List<LastChatMessage>> GetLastMessagesAsync(
int maxResultCount = 10,
CancellationToken cancellationToken = default)
{
sorting ??= $"{nameof(LastChatMessage.SendTime)} DESC";
if (sorting.IsNullOrWhiteSpace())
{
sorting = $"{nameof(LastChatMessage.SendTime)} DESC";
}
var dbContext = await GetDbContextAsync();

#region SQL 原型
Expand Down Expand Up @@ -439,13 +450,17 @@ public async virtual Task<List<UserMessage>> GetUserMessagesAsync(
int maxResultCount = 10,
CancellationToken cancellationToken = default)
{
if (sorting.IsNullOrWhiteSpace())
{
sorting = nameof(UserMessage.MessageId);
}
var userMessages = await (await GetDbContextAsync()).Set<UserMessage>()
.Where(x => (x.CreatorId.Equals(sendUserId) && x.ReceiveUserId.Equals(receiveUserId)) ||
x.CreatorId.Equals(receiveUserId) && x.ReceiveUserId.Equals(sendUserId))
.WhereIf(type.HasValue, x => x.Type.Equals(type))
.Where(x => x.State == MessageState.Send || x.State == MessageState.Read)
.WhereIf(!filter.IsNullOrWhiteSpace(), x => x.Content.Contains(filter) || x.SendUserName.Contains(filter))
.OrderBy(sorting ?? nameof(UserMessage.MessageId))
.OrderBy(sorting)
.PageBy(skipCount, maxResultCount)
.AsNoTracking()
.ToListAsync(GetCancellationToken(cancellationToken));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,16 @@ public async virtual Task<List<UserCard>> GetMembersAsync(
int maxResultCount = 10,
CancellationToken cancellationToken = default)
{
if (sorting.IsNullOrWhiteSpace())
{
sorting = nameof(UserChatCard.UserId);
}
return await (await GetDbSetAsync())
.WhereIf(!findUserName.IsNullOrWhiteSpace(), ucc => ucc.UserName.Contains(findUserName))
.WhereIf(startAge.HasValue, ucc => ucc.Age >= startAge.Value)
.WhereIf(endAge.HasValue, ucc => ucc.Age <= endAge.Value)
.WhereIf(sex.HasValue, ucc => ucc.Sex == sex)
.OrderBy(sorting ?? nameof(UserChatCard.UserId))
.OrderBy(sorting)
.PageBy(skipCount, maxResultCount)
.Select(ucc => ucc.ToUserCard())
.ToListAsync(GetCancellationToken(cancellationToken));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public async virtual Task<List<UserFriend>> GetAllMembersAsync(
string sorting = nameof(UserChatFriend.RemarkName),
CancellationToken cancellationToken = default)
{
if (sorting.IsNullOrWhiteSpace())
{
sorting = nameof(UserChatFriend.RemarkName);
}
var dbContext = await GetDbContextAsync();
var userFriendQuery = from ucf in dbContext.Set<UserChatFriend>()
join ucc in dbContext.Set<UserChatCard>()
Expand Down Expand Up @@ -60,7 +64,7 @@ on ucf.UserId equals ucc.UserId
};

return await userFriendQuery
.OrderBy(sorting ?? nameof(UserChatFriend.RemarkName))
.OrderBy(sorting)
.ToListAsync(GetCancellationToken(cancellationToken));
}

Expand Down Expand Up @@ -103,6 +107,10 @@ public async virtual Task<List<UserFriend>> GetMembersAsync(
int maxResultCount = 10,
CancellationToken cancellationToken = default)
{
if (sorting.IsNullOrWhiteSpace())
{
sorting = nameof(UserChatFriend.UserId);
}
var dbContext = await GetDbContextAsync();
// 过滤用户资料
var userChatCardQuery = dbContext.Set<UserChatCard>()
Expand Down Expand Up @@ -139,7 +147,7 @@ on ucf.FrientId equals ucc.UserId
};

return await userFriendQuery
.OrderBy(sorting ?? nameof(UserChatFriend.UserId))
.OrderBy(sorting)
.PageBy(skipCount, maxResultCount)
.ToListAsync(GetCancellationToken(cancellationToken));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LINGYUN.Abp.MessageService.EntityFrameworkCore;
using LINGYUN.Abp.MessageService.Chat;
using LINGYUN.Abp.MessageService.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -38,10 +39,14 @@ public async virtual Task<List<ChatGroup>> GetListAsync(
int maxResultCount = 10,
CancellationToken cancellationToken = default)
{
if (sorting.IsNullOrWhiteSpace())
{
sorting = nameof(ChatGroup.Name);
}
return await (await GetDbSetAsync())
.WhereIf(!filter.IsNullOrWhiteSpace(), x =>
x.Name.Contains(filter) || x.Tag.Contains(filter))
.OrderBy(sorting ?? nameof(ChatGroup.Name))
.OrderBy(sorting)
.PageBy(skipCount, maxResultCount)
.ToListAsync(GetCancellationToken(cancellationToken));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public async virtual Task<List<GroupUserCard>> GetMembersAsync(
int maxResultCount = 10,
CancellationToken cancellationToken = default)
{
if (sorting.IsNullOrWhiteSpace())
{
sorting = nameof(UserChatCard.UserId);
}
var dbContext = await GetDbContextAsync();
var cardQuery = from gp in dbContext.Set<ChatGroup>()
join ucg in dbContext.Set<UserChatGroup>()
Expand Down Expand Up @@ -91,7 +95,7 @@ on ugc.UserId equals uc.UserId
};

return await cardQuery
.OrderBy(sorting ?? nameof(UserChatCard.UserId))
.OrderBy(sorting)
.PageBy(skipCount, maxResultCount)
.ToListAsync(GetCancellationToken(cancellationToken));
}
Expand Down
Loading

0 comments on commit cd23384

Please sign in to comment.