public JObject ListGood(ListReceiveDto dto) { JObject jo = new JObject(); JArray container = new JArray(); string name = dto.Name; string tag = dto.Tag; string state = dto.State; List <ListGoodDto> TempList = new List <ListGoodDto>(); Regex NumberType = new Regex("^[0-9]*$"); if (tag != "" && state != "") { var tagId = WebapiDbContext.Tags.Where(o => o.Tag_name == tag).FirstOrDefault().Id; if (name != null && name != "" && NumberType.IsMatch(name)) { TempList = WebapiDbContext.GoodTags.Where(o => o.Tag_id == tagId).Join( WebapiDbContext.Goods, o => o.Good_id, p => p.Id, (o, p) => new ListGoodDto { Name = p.Name, Number = p.Number, Price = p.Price, State = p.State, Created_at = p.Created_at, Updated_at = p.Updated_at }).Where(o => o.Number == name && o.State == state) .OrderByDescending(o => o.Updated_at).Skip((dto.CurrentPage - 1) * dto.PageSize) .Take(dto.PageSize).ToList(); } else if (name != null && name != "") { TempList = WebapiDbContext.GoodTags.Where(o => o.Tag_id == tagId).Join( WebapiDbContext.Goods, o => o.Good_id, p => p.Id, (o, p) => new ListGoodDto { Name = p.Name, Number = p.Number, Price = p.Price, State = p.State, Created_at = p.Created_at, Updated_at = p.Updated_at }).Where(o => o.Name.Contains(name) && o.State == state) .OrderByDescending(o => o.Updated_at).Skip((dto.CurrentPage - 1) * dto.PageSize) .Take(dto.PageSize).ToList(); if (TempList.Count == 0) { PinYin py = new PinYin(); TempList = WebapiDbContext.GoodTags.Where(o => o.Tag_id == tagId).Join( WebapiDbContext.Goods, o => o.Good_id, p => p.Id, (o, p) => new ListGoodDto { Name = p.Name, Number = p.Number, Price = p.Price, State = p.State, Created_at = p.Created_at, Updated_at = p.Updated_at }).Where(o => py.GetPinyin(o.Name).Replace(" ", "").Trim().Contains(name.Replace(" ", "").Trim().ToLower()) && o.State == state) .OrderByDescending(o => o.Updated_at).Skip((dto.CurrentPage - 1) * dto.PageSize) .Take(dto.PageSize).ToList(); } } else { TempList = WebapiDbContext.GoodTags.Where(o => o.Tag_id == tagId).Join( WebapiDbContext.Goods, o => o.Good_id, p => p.Id, (o, p) => new ListGoodDto { Name = p.Name, Number = p.Number, Price = p.Price, State = p.State, Created_at = p.Created_at, Updated_at = p.Updated_at }).Where(o => o.State == state) .OrderByDescending(o => o.Updated_at).Skip((dto.CurrentPage - 1) * dto.PageSize) .Take(dto.PageSize).ToList(); } } else if (tag == "" && state != "") { if (name != null && name != "" && NumberType.IsMatch(name)) { TempList = WebapiDbContext.Goods.Where(o => o.Number == name && o.State == state) .OrderByDescending(o => o.Updated_at).Skip((dto.CurrentPage - 1) * dto.PageSize) .Take(dto.PageSize).Select(o => new ListGoodDto { Name = o.Name, Number = o.Number, Price = o.Price, State = o.State, Created_at = o.Created_at, Updated_at = o.Updated_at }).ToList(); } else if (name != null && name != "") { TempList = WebapiDbContext.Goods.Where(o => o.Name.Contains(name) && o.State == state) .OrderByDescending(o => o.Updated_at).Skip((dto.CurrentPage - 1) * dto.PageSize) .Take(dto.PageSize).Select(o => new ListGoodDto { Name = o.Name, Number = o.Number, Price = o.Price, State = o.State, Created_at = o.Created_at, Updated_at = o.Updated_at }).ToList(); if (TempList.Count == 0) { PinYin py = new PinYin(); TempList = WebapiDbContext.Goods.Where(o => py.GetPinyin(o.Name).Replace(" ", "").Trim().Contains(name.Replace(" ", "").Trim().ToLower()) && o.State == state) .OrderByDescending(o => o.Updated_at).Skip((dto.CurrentPage - 1) * dto.PageSize) .Take(dto.PageSize).Select(o => new ListGoodDto { Name = o.Name, Number = o.Number, Price = o.Price, State = o.State, Created_at = o.Created_at, Updated_at = o.Updated_at }).ToList(); } } else { TempList = WebapiDbContext.Goods.Where(o => o.State == state) .OrderByDescending(o => o.Updated_at).Skip((dto.CurrentPage - 1) * dto.PageSize) .Take(dto.PageSize).Select(o => new ListGoodDto { Name = o.Name, Number = o.Number, Price = o.Price, State = o.State, Created_at = o.Created_at, Updated_at = o.Updated_at }).ToList(); } } else if (tag != "" && state == "") { var tagId = WebapiDbContext.Tags.Where(o => o.Tag_name == tag).FirstOrDefault().Id; if (name != null && name != "" && NumberType.IsMatch(name)) { TempList = WebapiDbContext.GoodTags.Where(o => o.Tag_id == tagId).Join( WebapiDbContext.Goods, o => o.Good_id, p => p.Id, (o, p) => new ListGoodDto { Name = p.Name, Number = p.Number, Price = p.Price, State = p.State, Created_at = p.Created_at, Updated_at = p.Updated_at }).Where(o => o.Number == name) .OrderByDescending(o => o.Updated_at).Skip((dto.CurrentPage - 1) * dto.PageSize) .Take(dto.PageSize).ToList(); } else if (name != null && name != "") { TempList = WebapiDbContext.GoodTags.Where(o => o.Tag_id == tagId).Join( WebapiDbContext.Goods, o => o.Good_id, p => p.Id, (o, p) => new ListGoodDto { Name = p.Name, Number = p.Number, Price = p.Price, State = p.State, Created_at = p.Created_at, Updated_at = p.Updated_at }).Where(o => o.Name.Contains(name)) .OrderByDescending(o => o.Updated_at).Skip((dto.CurrentPage - 1) * dto.PageSize) .Take(dto.PageSize).ToList(); if (TempList.Count == 0) { PinYin py = new PinYin(); TempList = WebapiDbContext.GoodTags.Where(o => o.Tag_id == tagId).Join( WebapiDbContext.Goods, o => o.Good_id, p => p.Id, (o, p) => new ListGoodDto { Name = p.Name, Number = p.Number, Price = p.Price, State = p.State, Created_at = p.Created_at, Updated_at = p.Updated_at }).Where(o => py.GetPinyin(o.Name).Replace(" ", "").Trim().Contains(name.Replace(" ", "").Trim().ToLower())) .OrderByDescending(o => o.Updated_at).Skip((dto.CurrentPage - 1) * dto.PageSize) .Take(dto.PageSize).ToList(); } } else { TempList = WebapiDbContext.GoodTags.Where(o => o.Tag_id == tagId).Join( WebapiDbContext.Goods, o => o.Good_id, p => p.Id, (o, p) => new ListGoodDto { Name = p.Name, Number = p.Number, Price = p.Price, State = p.State, Created_at = p.Created_at, Updated_at = p.Updated_at }).OrderByDescending(o => o.Updated_at).Skip((dto.CurrentPage - 1) * dto.PageSize) .Take(dto.PageSize).ToList(); } } else { if (name != null && name != "" && NumberType.IsMatch(name)) { TempList = WebapiDbContext.Goods.Where(o => o.Number == name) .OrderByDescending(o => o.Updated_at).Skip((dto.CurrentPage - 1) * dto.PageSize) .Take(dto.PageSize).Select(o => new ListGoodDto { Name = o.Name, Number = o.Number, Price = o.Price, State = o.State, Created_at = o.Created_at, Updated_at = o.Updated_at }).ToList(); } else if (name != null && name != "") { TempList = WebapiDbContext.Goods.Where(o => o.Name.Contains(name)) .OrderByDescending(o => o.Updated_at).Skip((dto.CurrentPage - 1) * dto.PageSize) .Take(dto.PageSize).Select(o => new ListGoodDto { Name = o.Name, Number = o.Number, Price = o.Price, State = o.State, Created_at = o.Created_at, Updated_at = o.Updated_at }).ToList(); if (TempList.Count == 0) { PinYin py = new PinYin(); TempList = WebapiDbContext.Goods.Where(o => py.GetPinyin(o.Name).Replace(" ", "").Trim().Contains(name.Replace(" ", "").Trim().ToLower())) .OrderByDescending(o => o.Updated_at).Skip((dto.CurrentPage - 1) * dto.PageSize) .Take(dto.PageSize).Select(o => new ListGoodDto { Name = o.Name, Number = o.Number, Price = o.Price, State = o.State, Created_at = o.Created_at, Updated_at = o.Updated_at }).ToList(); } } else { TempList = WebapiDbContext.Goods.OrderByDescending(o => o.Updated_at).Skip((dto.CurrentPage - 1) * dto.PageSize) .Take(dto.PageSize).Select(o => new ListGoodDto { Name = o.Name, Number = o.Number, Price = o.Price, State = o.State, Created_at = o.Created_at, Updated_at = o.Updated_at }).ToList(); } } foreach (ListGoodDto temp in TempList) { JObject j = new JObject() { ["Name"] = temp.Name, ["Number"] = temp.Number, ["Price"] = temp.Price, ["State"] = temp.State, ["Created_at"] = temp.Created_at, ["Updated_at"] = temp.Updated_at, }; container.Add(j); } jo["stateCode"] = 200; jo["message"] = container; return(jo); }