LINQ获取某个表中的部分字段值
我有个新闻表
id,title,body,createtime,author,click
使用ef4.1 仅仅读取 id,title,createtime 并显示在页面上。
public static List<NewInfo> GetHotNews() { List<NewInfo> list; list = (from n in db.NewInfoes where n.IsTop == 1 orderby n.PublishTime descending select new { Title = n.Title, NewID = n.NewID, PublishTime = n.PublishTime }).ToList(); return list; } 改为
public static List<NewInfo> GetHotNews() { List<NewInfo> list; list = (from n in db.NewInfoes where n.IsTop == 1 orderby n.PublishTime descending .Select(n => new NewInfo { Title = n.Title, NewID = n.NewID, PublishTime = n.PublishTime }); return list; }