Linq to sql 语法方法示例
2024-10-19 00:19:55
联表查询,判断追加条件,对集合分页
public ActionResult Index(string titleName, string nickName, int pageIndex = )
{
var data = from m in _db.AppArticleComment
join o in _db.AppArticle on m.ArticleId equals o.ArticleId
where (string.IsNullOrEmpty(nickName) ? true : m.NickName == nickName)//在此判断加条件,当变量值null需要转为整型时会抱异常
select new AppArticleCommentModel()
{
Id = m.Id,
OId = m.OId,
MId = m.MId,
NickName = m.NickName,
Comment = m.Comment,
Good = m.Good,
Created = m.Created,
IsHorizontal = m.IsHorizontal,
Version = m.Version,
From = m.From,
ArticleName = o.Title }; if (!string.IsNullOrEmpty(titleName))
{
//也可在此判断添追加条件
data = data.Where(m => m.ArticleName.Contains(titleName));
} int total = ;
int pageSize = ;
//对集合进行分页
var query = data.OrderByDescending(m => m.Created)
.Skip(pageSize * (pageIndex - )).Take(pageSize); var list = query.ToList();
total = data.Count(); PagingHelper<AppArticleCommentModel> dataPaging = new PagingHelper<AppArticleCommentModel>(, list);//初始化分页器
dataPaging.PageIndex = pageIndex;//指定当前页
dataPaging.PageCount = (int)Math.Ceiling(total / (double)pageSize);
dataPaging.PageSize = pageSize;
dataPaging.TotalCount = total; return View(dataPaging);//返回分页器实例到视图
}
最新文章
- LDAP binary字段读取
- mysql实验
- MS SQL SERVER索引优化相关查询
- 边工作边刷题:70天一遍leetcode: day 82-1
- PHP json_decode object时报错Cannot use object of type stdClass as array
- C#连接mysql实例
- codeforce 702D Road to Post Office 物理计算路程题
- Ext入门学习系列(五)表格控件(1)
- 【POJ1182】 食物链 (带权并查集)
- 烟雾检测笔记1--《Video-based smoke detection with histogram sequence of LBP and LBPV pyramids》解析、实现
- 教训:TOJ[4081] God Le wants to know the directory
- 《JS正则表达式》
- Spring源码情操陶冶-tx:advice解析器
- 0326 iframe和video experience
- day 54 jQuery, part-1
- 英语口语练习系列-C29-鸟类-辨别身份-断弦的琴
- 部署MySQL自动化运维工具inception+archer
- SELECT版FTP
- Python 练习:简单的购物车
- 架构 : 三层架构、MVC、MVP、MVVM