Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

惰性加载查询,可以使用join或left了 #160

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions YiSha.Business/YiSha.Service/OrganizationManage/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,19 @@ public async Task<List<UserEntity>> GetList(UserListParam param)

public async Task<List<UserEntity>> GetPageList(UserListParam param, Pagination pagination)
{

var expression = ListFilter(param);
var list = await this.BaseRepository().FindList(expression, pagination);
return list.ToList();
//var list = await this.BaseRepository().FindList(expression, pagination);

//jhzou120230902 *******惰性加载查询,可以使用join或left了***
var ilist = this.BaseRepository().FindListLinq(expression);
pagination.TotalCount = ilist.Count();
var pageData = (
from i in ilist.OrderByDescending(s => s.BaseCreateTime).Skip(pagination.SkipCount).Take(pagination.PageSize)
select i
).ToList();

return pageData;
}

public async Task<UserEntity> GetEntity(long id)
Expand Down
11 changes: 11 additions & 0 deletions YiSha.Data/YiSha.Data.EF/Database/MySqlDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,5 +421,16 @@ public async Task<T> FindObject<T>(string strSql) where T : class
return list.FirstOrDefault();
}
#endregion


#region Linq惰性加载方式查询

public IEnumerable<T> FindListLinq<T>(Expression<Func<T, bool>> condition) where T : class, new()
{
IEnumerable<T> data = dbContext.Set<T>().Where(condition);
return data;
}

#endregion
}
}
12 changes: 12 additions & 0 deletions YiSha.Data/YiSha.Data.EF/Database/SqlServerDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,18 @@ public async Task<T> FindObject<T>(string strSql) where T : class
var list = await dbContext.SqlQuery<T>(strSql);
return list.FirstOrDefault();
}

/// <summary>
/// Linq惰性加载方式查询
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="condition"></param>
/// <returns></returns>
public IEnumerable<T> FindListLinq<T>(Expression<Func<T, bool>> condition) where T : class, new()
{
IEnumerable<T> data = dbContext.Set<T>().Where(condition);
return data;
}
#endregion
}
}
12 changes: 12 additions & 0 deletions YiSha.Data/YiSha.Data.Repository/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,18 @@ public async Task<IEnumerable<T>> FindList<T>(string strSql, DbParameter[] dbPar
pagination.TotalCount = data.total;
return data.Item2;
}


/// <summary>
/// Linq查询
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="condition"></param>
/// <returns></returns>
public IEnumerable<T> FindListLinq<T>(Expression<Func<T, bool>> condition) where T : class, new()
{
return db.FindListLinq<T>(condition);
}
#endregion

#region 数据源 查询
Expand Down
8 changes: 8 additions & 0 deletions YiSha.Data/YiSha.Data/IDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ public interface IDatabase
Task<object> FindObject(string strSql);
Task<object> FindObject(string strSql, DbParameter[] dbParameter);
Task<T> FindObject<T>(string strSql) where T : class;

/// <summary>
/// 惰性加载查询
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="condition"></param>
/// <returns></returns>
IEnumerable<T> FindListLinq<T>(Expression<Func<T, bool>> condition) where T : class, new();
#endregion
}
}
5 changes: 5 additions & 0 deletions YiSha.Util/YiSha.Util/Model/Pagination.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,10 @@ public int TotalPage
}
}
}

/// <summary>
/// 需跳过的行数
/// </summary>
public int SkipCount => PageSize * (PageIndex > 0 ? PageIndex - 1 : 0);
}
}
4 changes: 2 additions & 2 deletions YiSha.Web/YiSha.Admin.Web/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"ApiSite": "http://localhost:5001", // Api地址,例如可以上传文件到Api
"VirtualDirectory": "/admin", // 虚拟目录

"DBProvider": "SqlServer",
"DBConnectionString": "Server=localhost;User Id=sa;Password=hmAdmin@;Database=YsData;",
"DBProvider": "MySql",
"DBConnectionString": "server=localhost;database=carservicesdbtest;user=test;password=Abcd!2345;port=3306;pooling=true;max pool size=20;persist security info=True;charset=utf8;",
"DBCommandTimeout": 180, // 数据库超时时间,单位秒
"DBBackup": "", // 数据库备份路径

Expand Down
2 changes: 1 addition & 1 deletion YiSha.Web/YiSha.Admin.Web/wwwroot/yisha/js/yisha.min.js

Large diffs are not rendered by default.