zoukankan      html  css  js  c++  java
  • 【收集】Linq 查询方法收集

         开始不间断收集linq查询使用的方法,日拱一卒,月可渡江。

    1. 查询排序取前6个
      var newshops = storeDB.Shops.OrderByDescending(a => a.JoinDate).Take(6).ToList();
    2. 查询列表 常用用法
      var order = (from u in storeDB.Orders
                   where u.OrderId == id
                   select u).ToList();
    3. Find ()
      foreach (OrderDetail od in order_detail)
      {                                       
      var dish = storeDB.Dishs.Find(od.DishId);
      }
    4. First()
      var b = (from a in storeDB.Orders
               select a).First(x => x.OrderId.Equals(id));
    5. Any()

      用于判断集合中是否有元素满足某一条件;不延迟。(若条件为空,则集合只要不为空就返回True,否则为False)。
      bool isValid = storeDB.Orders.Any(o => o.OrderId == id && o.Username == User.Identity.Name); 
    6. Take(int n);
      var shops = (from u in storeDB.Shops
                   where u.TitleShopValue > 0
                   orderby u.TitleShopValue descending
                   select u).Take(8).ToList();

      取排前面的N个。

    7. Contains();提供模糊查询
       var shops = (from u in DB.Shops
             where u.Name.Contains(searchKey)
             orderby u.TitleShopValue descending
             select u).ToList();
    8. var id =storeDB.Users.SingleOrDefault(u => u.UserName ==model.UserName).UserID;
    9. 判断,返回bool类型
      return (from s in roles from userrole in userroles where s.Equals(userrole) select s).Any();
      
    10. 条件查询
      IList<UserFollow> Follows = db.UserFollows.Where(o => o.UserId == userid).ToList();
      
    11. 统计计数
      var ageCoun = (from s in GetStudents()  select s.Age).Count(a=>a>22);  
      
      Var Count = LDB.User.Count(o=>o.age == 23);
      





    12.  
  • 相关阅读:
    JAVA Aes加密解密
    15个实用的jQuery代码片
    mybatis-generator-config工具的使用
    Oracle中Merge into用法总结
    Highcharts 在低版本 IE 上使用注意事项及个人总结
    梦想是什么
    博客美化基本后台设置与样式设置
    IntelliJ Idea 常用快捷键列表
    磁盘
    [半转]1px边框 移动端
  • 原文地址:https://www.cnblogs.com/Raywang80s/p/2796072.html
Copyright © 2011-2022 走看看