52 lines
1.4 KiB
C#
52 lines
1.4 KiB
C#
using PagedList;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Entity;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
using System.Web.Http;
|
|
|
|
/// <summary>
|
|
/// lianyouController 的摘要描述
|
|
/// </summary>
|
|
[ezAuthorize]
|
|
public class lianyouController : ApiController
|
|
{
|
|
private Model.ezEntities _db = new Model.ezEntities();
|
|
|
|
[HttpPost]
|
|
[Route("api/lianyou/getfollowers")]
|
|
public async Task<IHttpActionResult> GetGuadanFollowers(int page, int pageSize, string searchName = null)
|
|
{
|
|
// IQueryable 可讓 EF 在資料庫層面執行過濾和投影
|
|
var qry = _db.followers.AsQueryable();
|
|
|
|
if (!string.IsNullOrEmpty(searchName))
|
|
{
|
|
qry = qry.Where(a => (a.u_name ?? "").Contains(searchName) || (a.phone ?? "").Contains(searchName));
|
|
}
|
|
|
|
var count = await qry.CountAsync();
|
|
|
|
// 投影到只需要的欄位
|
|
var projected = qry
|
|
.OrderBy(a => a.f_number)
|
|
.Select(a => new
|
|
{
|
|
num = a.num,
|
|
u_name = a.u_name,
|
|
phone = a.phone,
|
|
sex = a.sex,
|
|
});
|
|
|
|
// 分頁
|
|
var data = projected
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToList(); // 如果使用 EF Core 可用 ToListAsync()
|
|
|
|
return Ok(new { data = data, count = count });
|
|
}
|
|
|
|
} |