
[英]How to get row count in asp.net and transfer asp.net row count to javascript?
[英]How to get Category Count Use at News in asp.net
大家好,我在获取新闻中类别使用的数量时遇到问题
例如,我想知道“国际”类别在新闻中有多少使用
这是我的新闻实体:
public class News
{
public int NewsId { get; set; }
public int? SubGroup { get; set; }
public string NewsTitle { get; set; }
public string NewsBody { get; set; }
public DateTime CreatedDate { get; set; }
public string Poster { get; set; }
public DateTime? UpdateTime { get; set; }
public bool isChoseClerck{ get; set; }
public News()
{
this.CreatedDate = DateTime.Now;
}
//RelationShip
public List<Category> Category { get; set; }
public User User { get; set; }
public List<Tag> Tags { get; set; }
public string Source{ get; set; }
}
这是我想向用户展示的 Dto:
public class SubCategoryWithCountDTO
{
public int CategoryId { get; set; }
public string CateGoryName { get; set; }
public int UseCount { get; set; }
}
我在服务下面写过我不知道我不能在“包含”中使用 lambda
public async Task<List<SubCategoryWithCountDTO>> SubCategoryWithCount(int skip, int take)
{
var subCategoryList = await _context.Categories
.Where(x => x.ParentId != null).OrderBy(x => x.CategoryId)
.Skip(skip).Take(take).ToListAsync();
var SubCategoryWithCount = new List<SubCategoryWithCountDTO>();
foreach (var item in subCategoryList)
{
var categoryUse = _context.News.Include(x => x.Category
.Select(x => x.CategoryId == item.CategoryId)).Count();
var preSubCategory = new SubCategoryWithCountDTO
{
CategoryId = item.CategoryId,
CateGoryName = item.CateGoryName,
UseCount = categoryUse
};
SubCategoryWithCount.Add(preSubCategory);
}
return SubCategoryWithCount;
}
有人可以告诉我正确的方法吗?谢谢
我对我的服务进行了一些更改,并使用了其他一些方法来计算,是的,它确实有效
public async Task<List<SubCategoryWithCountDTO>> SubCategoryWithCount(int skip, int take)
{
var subCategoryList = await _context.Categories
.Where(x => x.ParentId != null).OrderBy(x => x.CategoryId)
.Skip(skip).Take(take).ToListAsync();
var SubCategoryWithCount = new List<SubCategoryWithCountDTO>();
var news = _context.News.Include(x => x.Category);
foreach (var item in subCategoryList)
{
var categoryUse = news.SelectMany(x => x.Category.Where(x => x.CategoryId == item.CategoryId)).CountAsync();
var preSubCategory = new SubCategoryWithCountDTO
{
CategoryId = item.CategoryId,
CateGoryName = item.CateGoryName,
UseCount = await categoryUse
};
SubCategoryWithCount.Add(preSubCategory);
}
return SubCategoryWithCount;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.