这个接口本来是分页的,前端要求不分页,结果1000条记录显示需要1分47秒怎么办

goodgame365 2021-01-12 04:24:09
大家看看,是不是我写的有问题,里面循环读取调用数据至少三次


public string getOneRoomOnePriceListByPage(int houseid, int bid = 0, int uid = 0,int HouseTypeID=0, int pageindex = 1, int pagesize = 10)
{
int erro =1;
JObject jOb = new JObject();

ArrayList List = new ArrayList();

int totalCount = 0;

DataTable dt = AppWXsource.getOneRoomOnePriceListPageByHouseID(houseid);
DataRow[] drArr = null;

if (dt != null&&dt.Rows.Count>0)
{
erro = 1;
if (bid == 0 && uid == 0 && HouseTypeID == 0)
drArr = dt.Select("HouseID=" + houseid);//查询
else if (bid != 0 && uid == 0 && HouseTypeID == 0)
drArr = dt.Select("BuildingID=" + bid);//查询
else if (bid != 0 && uid != 0 && HouseTypeID == 0)
drArr = dt.Select("UnitID=" + uid+ " and BuildingID=" + bid);//查询
else if (bid != 0 && uid != 0 && HouseTypeID != 0)
drArr = dt.Select("HouseTypeID=" + HouseTypeID+ " and UnitID = " + uid + " and BuildingID=" + bid);//查询
if (drArr==null||drArr.Length<=0)
{
jOb.Add(new JProperty("total", totalCount));
jOb.Add(new JProperty("list", List));
return JsonDo.JobjectToJson(jOb,erro, "没有相关的记录");

}

totalCount = drArr.Length;//取得总数


int pagenum = 1;
int lastnum = 0;
pagenum = pagesize * (pageindex - 1);
if ((pagenum + pagesize) > totalCount)
lastnum = totalCount;
else
lastnum = pagenum + pagesize;

for (int i= pagenum; i < lastnum; i++)
{
DataRow rs = drArr[i];

//取得房号,房号户型ID
int RoomID =(int)rs["RoomID"];
string RoomNum = "";
string Rooms = "";
string Halls = "";
string Floors = "";

DataTable Rtd = new DAL.AppWX.room().getRoomAndHouseTypeInfoByRoomID(RoomID);

if(Rtd!=null&&Rtd.Rows.Count>0)
{
RoomNum = Rtd.Rows[0]["RoomNum"].ToString();
Rooms = Rtd.Rows[0]["Rooms"].ToString();
Halls = Rtd.Rows[0]["Halls"].ToString();
Floors = Rtd.Rows[0]["Floors"].ToString();
}

JObject jObj = new JObject();
jObj.Add(new JProperty("SourceID", rs["SourceID"]));
jObj.Add(new JProperty("RoomNum", RoomNum));


var SourceTag = rs["SourceTag"].ToString();
if (SourceTag != "")
{
SourceTag = SourceTag.TrimEnd(',');
SourceTag = new BLL.Source().GetOneHouseAllTagsName(SourceTag.Trim());
}

jObj.Add(new JProperty("SourceTag", SourceTag));

//读取户型资料
string Tid = rs["HouseTypeID"].ToString();
string typeinfo = "";

typeinfo = Rooms + "房" + Halls + "厅";
jObj.Add(new JProperty("housetypes", typeinfo));
jObj.Add(new JProperty("CoveredArea", rs["CoveredArea"]));
jObj.Add(new JProperty("Floors", Floors));

float a = 0;
float b = 0;
float totprice = 0;
float oneprice = 0;
try
{
a = float.Parse(rs["SourcePrice"].ToString());
b = float.Parse(rs["CoveredArea"].ToString());
int pricetype = int.Parse(rs["pricetype"].ToString());

if (pricetype == 2)//2为单价
{
totprice = a * b;
oneprice = a;
}
else
{
totprice = a;
oneprice = a / b;
}
}
catch
{
//
}

//取得房源首图
string SourceImg = "";
int SourceID = int.Parse(rs["sourceID"].ToString());
DataTable dtImg = AppWXsource.getSourceImg(SourceID);
if (dtImg.Rows.Count > 0)
{
SourceImg = ImageUrl + dtImg.Rows[0]["ThumbnailName"].ToString();
}
else
{
//如果房源图片为空,则调取户型图
int houseTypeID = Common.Util.ConvertInt32(rs["HouseTypeID"], 0);
DataTable dtHouseTypeImg = new DAL.House().GetPhotoByHouseType(houseTypeID);
if (dtHouseTypeImg != null && dtHouseTypeImg.Rows.Count > 0)
{

SourceImg = ImageUrl + dtHouseTypeImg.Rows[0]["ThumbnailName"].ToString();


}

}
jObj.Add(new JProperty("SourceImg", SourceImg));

jObj.Add(new JProperty("oneprice", oneprice));
jObj.Add(new JProperty("totprice", totprice));
jObj.Add(new JProperty("IsOnline", rs["IsOnline"]));

jObj.Add(new JProperty("IsSold", rs["IsSold"]));
jObj.Add(new JProperty("HouseTypeID", rs["HouseTypeID"]));

//取得多少栋多少单元多少房号



DataTable dtS = AppWXsource.getHouseNameBuildingNameUnitNameRoomNum(RoomID);
if (dtS.Rows.Count > 0)
{

jObj.Add(new JProperty("HouseName", dtS.Rows[0]["HouseName"]));
jObj.Add(new JProperty("BuildingName", dtS.Rows[0]["BuildingName"]));
jObj.Add(new JProperty("UnitName", dtS.Rows[0]["UnitName"]));



}


List.Add(jObj);


}

}
jOb.Add(new JProperty("total", totalCount));
jOb.Add(new JProperty("list", List));

return JsonDo.JobjectToJson(jOb, erro);
}




...全文
4343 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
墨梅无痕 2021-01-19
  • 打赏
  • 举报
回复
没有什么问题,就是图片的处理效率,读取、传输、显示都需要时间,大量图片一定要异步处理。
  • 打赏
  • 举报
回复
你额外提供这类 api 其实就是替换前端去“背锅”。前端只要是给编程人员,他不会循环异步去读取数据、动态加载数据,他只会“调用一次api”,他怎么干开发啊?
  • 打赏
  • 举报
回复
提供分页的 api 就可以了。前端如果要全部数据,他应该自己将各页数据追加过去,与你无关。

你提供的分页 api 有什么问题吗?
gs0038 2021-01-14
  • 打赏
  • 举报
回复
看了一下,你一条记录包括了好几个查询数据库的哦 你仔细检查一下,可不可以修改一下,只做一次数据库查询,能不能用关系查询来实现
八爻老骥 2021-01-13
  • 打赏
  • 举报
回复
可以试试并行处理,按页同时请求,最后将结果合并。
dugupiaoyun 2021-01-13
  • 打赏
  • 举报
回复
自己代码里加个时间统计,看你返回JSON总共代码执行了多久。页面显示慢,不是你的问题,应该是前台自己没处理好,既然要求1000条数据不分页,那么应该是类似滚动触发加载,鼠标滚动就push一定量的数据到展示列表数据中。
ziqi0716 2021-01-12
  • 打赏
  • 举报
回复
同意楼上意见. 你把代码优化下,单个函数代码控制在20行以内,并对函数取有意义的名称.你做完这些,你的问题可能就已经解决了.
晨易夕 2021-01-12
  • 打赏
  • 举报
回复
这个函数一看两百行,连看你代码的兴趣都没了。

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧