• LINQ TO SQL 常用语法


    LINQ To SQL中IN的用法

     

    IN:

    C#

    var result = from s1 in context.Customers where (new string[]

                          { "UK", "Lisboa" }).Contains(s1.City) select s1;

    VB.NET

    Dim lists = From s1 In context.Customers Where (New String() {"UK", "Lisboa"}).Contains(s1.City) Select s1

    NOT IN:

    C#

      var result = from s1 in context.Customers where !(new string[] { "UK", "Lisboa" }).Contains(s1.City) select s1;

    VB.NET

          Dim lists = From s1 In context.Customers Where Not (New String() {"UK", "Lisboa"}).Contains(s1.City) Select s1      

    ------------------------------------------------------------------

    linq to sql 批量更新方法

    using (var db = new uc.UserCenter())
    {
    List<string> idList = id.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList<string>();
    //var ui = from item in db.Notice
    // where idList.Contains(item.NoticeID.ToString())
    // select item;
    var notices = from item in db.Notice.Where(a => idList.Contains(a.NoticeID.ToString()))
    select item;
    foreach (var item in notices)
    {
    item.Status = false;
    }
    db.SaveChanges();
    }

    更过常用方法见之里:http://www.cnblogs.com/coolcode/archive/2010/07/11/LinqToSQL_Batch_Update.html

    ------------------------------------------------

     分组统计  参考地址:http://social.msdn.microsoft.com/Forums/zh-CN/99fdaca7-1ad4-45ef-8bd4-00a1f9f9eb0d/linq-groupby-com-objetos-persistencia?forum=linqpt

    List<ModelStatUnReadNotice> statUnReadNotice = (from a in db.Notice.Where(w=>w.UserID == userId && w.IsRead == false && w.Type == type && w.Status == true)
    group a by new { a.Type } into tongji
    select new ModelStatUnReadNotice
    {
    Type = tongji.Key.Type.Value,
    NoticeCount = tongji.Count()
    }).ToList();

  • 相关阅读:
    Spring boot --- Spring Oauth(三)
    Spring boot --- Spring Oauth(一)
    JavaScript 学习(一)
    Springboot --- Spring Security (一)
    HTML 学习笔记 JavaScript (节点)
    HTML 学习笔记 JavaScript(事件)
    HTML 学习笔记 JavaScript (DOM)
    HTML 学习笔记 CSS3 (多列)
    HTML 学习笔记 CSS3(Animation)
    HTML 学习笔记 CSS3(过度 transition)
  • 原文地址:https://www.cnblogs.com/niaowo/p/3645286.html
Copyright © 2020-2023  润新知