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个数据
(模糊查询)
var quest = from qu in mdc.Question where qu.Question1.Contains("数") select qu;//模糊查询
--------qu.Question1.Contains("数")//Linq的表的哪列的Contains()的方法
(Linq通过对象向数据库添加数据)
NewsInfo newnf = new NewsInfo();
newnf.newscontext = nf.newscontext;
newnf.newsauthor = nf.newsauthor;
newnf.newstime = DateTime.Now;//得到系统时间
mdc.NewsInfo.InsertOnSubmit(newnf);//标记添加状态
mdc.SubmitChanges();//执行添加
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扩展函数
var names = from c in myCars select c.PetName;
foreach (var n in names)
{
Console.WriteLine("Name: {0}", n);
}
也可以在兼容的数组类型上调用
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" select c;
var onlyFastBMWs = from c in myCars
where c.Make == "BMW" && c.Speed >= 100 select c;
5、生成新的数据类型(投影)
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>
/// <param name="news"></param>
public void AddNews(News news) {
gameLinq.News.InsertOnSubmit(news);
gameLinq.SubmitChanges();
}
/// <summary>
/// 根据ID删除一天新闻
/// </summary>
/// <param name="id"></param>
public void DeleteNews(int id) {
var news = from p in gameLinq.News where p.NewsId == id select p;
gameLinq.News.DeleteAllOnSubmit(news);
gameLinq.SubmitChanges();
}
/// <summary>
/// 更新一天新闻
/// </summary>
/// <param name="n"></param>
public void UpdateNews(News n) {
var news = from p in gameLinq.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 UserInfo GetUserByUserid(string userid)
{
var u=from p in MyOfficeLinq.UserInfo where p.UserId==userid select p;
UserInfo user=u.First();
return user;
}
/// <summary>
/// 得到所以对象集合
/// </summary>
public List<RoleRight> GetAllRoleRight()
{
List<RoleRight> list = new List<RoleRight>();
var r = from p in MyOfficeLinq.RoleRight select p;
foreach (RoleRight item in r)
{
list.Add(item);
}
return list;
}