基本語(yǔ)法
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個(gè)數(shù)據(jù)
(模糊查詢(xún))
var quest = from qu in mdc.Question where qu.Question1.Contains("數(shù)") selectqu;//模糊查詢(xún) --------qu.Question1.Contains("數(shù)")//Linq的表的哪列的Contains()的方法
(Linq通過(guò)對(duì)象向數(shù)據(jù)庫(kù)添加數(shù)據(jù))
?
NewsInfo newnf = newNewsInfo(); ? ? ? ? ? newnf.newscontext =nf.newscontext; ? ? ? ? ? newnf.newsauthor =nf.newsauthor; ? ? ? ? ? newnf.newstime =DateTime.Now;//得到系統(tǒng)時(shí)間 ? ? ? ? ? mdc.NewsInfo.InsertOnSubmit(newnf);//標(biāo)記添加狀態(tài) ? ? ? ? ? mdc.SubmitChanges();//執(zhí)行添加
2、只獲取字段名稱(chēng)
var names = from c in myCars select c.PetName;//這里names就是隱式類(lèi)型的變量善镰。
3妹萨、使用Enumerable.Distinct<T>()
var makes = (from c in myCars select c.Make).Distinct<string>();
4、即可以在定義的時(shí)候調(diào)用Enumberalbe擴(kuò)展函數(shù)
var names = from c in myCars select c.PetName; foreach (var n in names) { Console.WriteLine("Name: {0}", n); }
也可以在兼容的數(shù)組類(lèi)型上調(diào)用
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ù)類(lèi)型(投影)
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、排序
默認(rèn)是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;
默認(rèn)順序時(shí)也可以明確指明
var subset = from c in myCars orderby c.PetName ascending select c;
8品洛、Enumerable.Except()
兩個(gè)IEnumerable<T>兼容的對(duì)象的差集
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> ? ? ? /// 得到一個(gè)對(duì)象 ? ? ? /// </summary> ? ? public UserInfoGetUserByUserid(string userid) ? ? ? { ? ? ? ? ? var u=from p inMyOfficeLinq.UserInfo where p.UserId==userid select p; ? ? ? ? ? UserInfouser=u.First(); ? ? ? ? ? return user; ? ? ? } ? ? ? ? ? ? ///<summary> ? ? ? /// 得到所以對(duì)象集合 ? ? ? /// </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; ? ? ? ? }