linq查询结果指定列的两种方式

方式一:

var results = from product in products
                orderby product.Price descending
                select new {
                    product.Name,
                    product.Price
                };

方式二:

products.Where(pro => pro.Price < 3M).Select(s => new { s.Name, s.Price });

查询结果转换到实体里:

方式一:

复制代码
var list = from q in _db.Page_AdPages
                       where q.IsDelete == "N"
                        orderby q.PageID
                        select new AdPages
                        {
                            PageID = q.PageID,
                            PageName = q.PageName,
                            Flag = (Int32)q.Flag
                        };
            return list.ToList<AdPages>();
复制代码

方式二:

复制代码
public List<Admin_CPcompany> GetCpList()
        {
            var result = _db.Admin_CPcompany.Select(s => new Admin_CPcompany { ID=s.ID,CPname=s.CPname,AddTime=s.AddTime,IsDelete=s.IsDelete}).ToList();
            return result;
        }
复制代码

 

posted on 2017-08-17 12:13  IT-HourseMan  阅读(556)  评论(0编辑  收藏  举报