Linq語法

基本語法

var result=from item in container orderby value ascending/descending? ? ? ? ? ? ? ? ?  select item;



1、獲取全部記錄

var allCars = from c in myCars select c;



(提取指定行記錄)

var allCars = (from c in myCars select c).Take<myCars>(5);//提取前5個數(shù)據(jù)



(模糊查詢)

var quest = from qu in mdc.Question where qu.Question1.Contains("數(shù)") selectqu;//模糊查詢

--------qu.Question1.Contains("數(shù)")//Linq的表的哪列的Contains()的方法



(Linq通過對象向數(shù)據(jù)庫添加數(shù)據(jù))

?

NewsInfo newnf = newNewsInfo();

? ? ? ? ?  newnf.newscontext =nf.newscontext;

? ? ? ? ?  newnf.newsauthor =nf.newsauthor;

? ? ? ? ?  newnf.newstime =DateTime.Now;//得到系統(tǒng)時間

? ? ? ? ? mdc.NewsInfo.InsertOnSubmit(newnf);//標記添加狀態(tài)

? ? ? ? ?  mdc.SubmitChanges();//執(zhí)行添加



2、只獲取字段名稱

var names = from c in myCars select c.PetName;//這里names就是隱式類型的變量。



3、使用Enumerable.Distinct<T>()

var makes = (from c in myCars select c.Make).Distinct<string>();



4、即可以在定義的時候調用Enumberalbe擴展函數(shù)

var names = from c in myCars select c.PetName;

foreach (var n in names)

{

Console.WriteLine("Name: {0}", n);

}



也可以在兼容的數(shù)組類型上調用

var makes = from c in myCars select c.Make;

Console.WriteLine("Distinct makes:");

foreach (var m in makes.Distinct<string>())

{

Console.WriteLine("Make: {0}", m);

}

var onlyBMWs = from c in myCars where c.Make == "BMW" selectc;

var onlyFastBMWs = from c in myCars

where c.Make == "BMW" && c.Speed >= 100 select c;



5、生成新的數(shù)據(jù)類型(投影)

var makesColors = from c in myCars select new {c.Make, c.Color};



6、Reverse<T>()

var subset = (from c in myCars select c).Reverse<Car>();

foreach (Car c in subset)

{

Console.WriteLine("{0} is going {1} MPH", c.PetName,c.Speed);

}



或者

var subset = from c in myCars select c;

foreach (Car c in subset.Reverse<Car>())

{

Console.WriteLine(c.ToString());

}



7、排序

默認是ascending

var subset = from c in myCars orderby c.PetName select c;

subset = from c in myCars

where c.Speed > 55 orderby c.PetName descending select c;


默認順序時也可以明確指明

var subset = from c in myCars

orderby c.PetName ascending select c;



8、Enumerable.Except()

兩個IEnumerable<T>兼容的對象的差集

static void GetDiff()

{

List<string> myCars = new List<String>

{ "Yugo", "Aztec", "BMW"};

List<string> yourCars = new List<String>

{ "BMW", "Saab", "Aztec" };

var carDiff =(from c in myCars select c)

.Except(from c2 in yourCars select c2);

Console.WriteLine("Here is what you don't have, but I do:");

foreach (string s in carDiff)

Console.WriteLine(s);

}

 

 

 

 

? ? ?  /// <summary>

? ? ?  /// 添加一挑新聞

? ? ?  /// </summary>

? ? ?  /// <paramname="news"></param>

? ? ?  public void AddNews(Newsnews) {

? ? ? ? ? gameLinq.News.InsertOnSubmit(news);

? ? ? ? ?  gameLinq.SubmitChanges();

? ? ?  }

? ? ?  /// <summary>

? ? ?  /// 根據(jù)ID刪除一天新聞

? ? ?  /// </summary>

? ? ?  /// <paramname="id"></param>

? ? ?  public void DeleteNews(intid) {

? ? ? ? ?  var news = from p ingameLinq.News where p.NewsId == id select p;

? ? ? ? ?  gameLinq.News.DeleteAllOnSubmit(news);

? ? ? ? ? gameLinq.SubmitChanges();

? ? ?  }

 

 

? ? ?  /// <summary>

? ? ?  /// 更新一天新聞

? ? ?  /// </summary>

? ? ?  /// <paramname="n"></param>

? ? ?  public voidUpdateNews(News n) {

? ? ? ? ?  var news = from p ingameLinq.News where p.NewsId == n.NewsId select p;

? ? ? ? ?  News va =news.First();

? ? ? ? ?  va.Content =n.Content;

? ? ? ? ?  va.NewsState =n.NewsState;

? ? ? ? ?  va.NewsTime =n.NewsTime;

? ? ? ? ?  va.Title = n.Title;

 

? ? ? ? ? gameLinq.SubmitChanges();

? ? ?  }

?  

 

 

? ? ?  /// <summary>

? ? ?  /// 得到一個對象

? ? ?  /// </summary>

? ?  public UserInfoGetUserByUserid(string userid)

? ? ?  {

? ? ? ? ?  var u=from p inMyOfficeLinq.UserInfo where p.UserId==userid select p;

? ? ? ? ?  UserInfouser=u.First();

? ? ? ? ?  return user;

? ? ?  }

? ? ? 

 

? ? ? ///<summary>

? ? ?  /// 得到所以對象集合

? ? ?  /// </summary>

? ? ? ? publicList<RoleRight> GetAllRoleRight()

? ? ? ? {

? ? ? ? ? ? List<RoleRight>list = new List<RoleRight>();

? ? ? ? ? ? var r = from p inMyOfficeLinq.RoleRight select p;

? ? ? ? ? ? foreach (RoleRightitem in r)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? list.Add(item);

? ? ? ? ? ? }

? ? ? ? ? ? return list;

? ? ? ? }

 

 
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 應用程序還需要操作存儲在其他數(shù)據(jù)源(如SQL數(shù)據(jù)庫或XML文件)中的數(shù)據(jù),甚至通過Web服務訪問它們。傳統(tǒng)上,查詢...
    CarlDonitz閱讀 677評論 0 0
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,602評論 19 139
  • 1. 簡介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存儲過程以及高級映射的優(yōu)秀的...
    笨鳥慢飛閱讀 6,248評論 0 4
  • 原文:https://my.oschina.net/liuyuantao/blog/751438 查詢集API 參...
    陽光小鎮(zhèn)少爺閱讀 3,963評論 0 8
  • Spark SQL, DataFrames and Datasets Guide Overview SQL Dat...
    Joyyx閱讀 8,494評論 0 16

友情鏈接更多精彩內(nèi)容