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();

你可能感兴趣的:(LINQ)