发布网友 发布时间:2022-04-27 13:17
共2个回答
懂视网 时间:2022-04-09 02:41
//1 var ss = from r in db.Am_recProScheme select r; //2 var ss1 = db.Am_recProScheme; //3 string sssql = "select * from Am_recProScheme";
//1 var ss = from r in db.Am_recProScheme where r.rpId > 10 select r; //2 var ss1 = db.Am_recProScheme.Where(p => p.rpId > 10); //3 string sssql = "select * from Am_recProScheme where rpid>10";
//1 ////获取最大的rpId //var ss = (from r in db.Am_recProScheme // select r).Max(p => p.rpId); ////获取最小的rpId //var ss = (from r in db.Am_recProScheme // select r).Min(p => p.rpId); //获取结果集的总数 //var ss = (from r in db.Am_recProScheme // select r).Count(); //获取rpId的和 var ss = (from r in db.Am_recProScheme select r).Sum(p => p.rpId); //2 //var ss1 = db.Am_recProScheme.Max(p=>p.rpId); //var ss1 = db.Am_recProScheme.Min(p => p.rpId); //var ss1 = db.Am_recProScheme.Count() ; var ss1 = db.Am_recProScheme.Sum(p => p.rpId); Response.Write(ss); //3 string sssql = "select max(rpId) from Am_recProScheme"; sssql = "select min(rpId) from Am_recProScheme"; sssql = "select count(1) from Am_recProScheme"; sssql = "select sum(rpId) from Am_recProScheme";
var ss = from r in db.Am_recProScheme where r.rpId > 10 orderby r.rpId descending //倒序 // orderby r.rpId ascending //正序 select r; //正序 var ss1 = db.Am_recProScheme.OrderBy(p => p.rpId).Where(p => p.rpId > 10).ToList(); //倒序 var ss2 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Where(p => p.rpId > 10).ToList(); string sssql = "select * from Am_recProScheme where rpid>10 order by rpId [desc|asc]";
//如果取最后一个可以按倒叙排列再取值 var ss = (from r in db.Am_recProScheme select r).FirstOrDefault(); //()linq to ef 好像不支持 Last() var ss1 = db.Am_recProScheme.FirstOrDefault(); //var ss1 = db.Am_recProScheme.First(); string sssql = "select top(1) * from Am_recProScheme";
//1 var ss = (from r in db.Am_recProScheme orderby r.rpId descending select r).Skip(10); //跳过前10条数据,取10条之后的所有数据 //2 var ss1 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Skip(10).ToList(); //3 string sssql = "select * from (select ROW_NUMBER()over(order by rpId desc) as rowNum, * from [Am_recProScheme]) as t where rowNum>10";
//1 var ss = (from r in db.Am_recProScheme where r.rpId > 10 orderby r.rpId descending select r).Skip(10).Take(10); //取第11条到第20条数据 //2 Take(10): 数据从开始获取,获取指定数量(10)的连续数据 var ss1 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Where(p => p.rpId > 10).Skip(10).Take(10).ToList(); //3 string sssql = "select * from (select ROW_NUMBER()over(order by rpId desc) as rowNum, * from [Am_recProScheme]) as t where rowNum>10 and rowNum<=20";
//1 var ss = from r in db.Am_recProScheme where r.SortsText.Contains("张") select r; //2 var ss1 = db.Am_recProScheme.Where(p => p.SortsText.Contains("张")).ToList(); //3 string sssql = "select * from Am_recProScheme where SortsText like ‘%张%‘";
//1 var ss = from r in db.Am_recProScheme orderby r.rpId descending group r by r.recType into n select new { n.Key, //这个Key是recType rpId = n.Sum(r => r.rpId), //组内rpId之和 MaxRpId = n.Max(r => r.rpId),//组内最大rpId MinRpId = n.Min(r => r.rpId), //组内最小rpId }; foreach (var t in ss) { Response.Write(t.Key + "--" + t.rpId + "--" + t.MaxRpId + "--" + t.MinRpId); } //2 var ss1 = from r in db.Am_recProScheme orderby r.rpId descending group r by r.recType into n select n; foreach (var t in ss1) { Response.Write(t.Key + "--" + t.Min(p => p.rpId)); } //3 var ss2 = db.Am_recProScheme.GroupBy(p => p.recType); foreach (var t in ss2) { Response.Write(t.Key + "--" + t.Min(p => p.rpId)); } //4 string sssql = "select recType,min(rpId),max(rpId),sum(rpId) from Am_recProScheme group by recType";
//1 var ss = from r in db.Am_recProScheme join w in db.Am_Test_Result on r.rpId equals w.rsId orderby r.rpId descending select r; //2 var ss1 = db.Am_recProScheme.Join(db.Am_Test_Result, p => p.rpId, r => r.rsId, (p, r) => p).OrderByDescending(p => p.rpId).ToList(); //3 string sssql = "select r.* from [Am_recProScheme] as r inner join [dbo].[Am_Test_Result] as t on r.[rpId] = t.[rsId] order by r.[rpId] desc";
//1 var ss = from p in db.Am_recProScheme where (new int?[] { 24, 25,26 }).Contains(p.rpId) select p; foreach (var p in ss) { Response.Write(p.Sorts); } //2 string st = "select * from Am_recProScheme where rpId in(24,25,26)";
Linq语法详细(三种方式:linq、Lambda、SQL语法)
标签:
热心网友 时间:2022-04-08 23:49
C#3.0 LINQ 查询语法
首先来看一个很简单的LINQ查询例子,查询一个int数组中小于5的数字,并按照大小顺序排列:
static void Main(string[] args)
{
int[] arr = new int[] { 8, 5, 89, 41, 1, 2, 3, 65, 1 };
var m = from n in arr where n < 5 orderby n descending select n;//小于5,并且倒叙排列显示
foreach (var n in m)
{
Console.WriteLine(n);
}
Console.ReadLine();
}
上述代码除了LINQ查询语法外,其他都是我们所熟悉的语法,而LINQ查询语法跟SQL查询语法很相似,除了先后顺序。
Q:为何 LINQ 查询语法是以 from 关键字开头的,而不是以 select 关键字开头的?select 开头这种写法跟SQL的写法更接近,更易懂呀?
A:简单来说,为了IDE的智能感知(Intellisence)这个功能,select 关键字放在后面了。
编程语言以 select 开头写LINQ查询语法不是没出现过,你如果使用过2005年的VB9 CTP 版本,那时候VB9的LINQ查询语法就是 select 关键字在前面,但是 select 关键字在前面,在做智能感知(Intelisence)时候就很头大。经过微软IDE组的权衡,确定了把 from 关键字放在最前面。
那时候 VB9 LINQ的查询语法还是 select 参数在最前面。不过后来 VB9 测试版改成了跟 C# 一样的做法, from 关键字放在最前面了。
更详细的解释,来自装配脑袋
假设你要书写这样的代码:Select p. Name, p.Age From p In persons Where xxx ,代码是一个个字符输入的。
我们在写到 p in persons 之前,p 的类型是无法推测的,所以写 Select p. 的时候,Name之类的属性不会弹出智能提示来。
这样就需要先去写 From 这句,再回来写 Select。
微软IDE组经过反复考虑决定,还不如就把 Select 写到后面了。于是编程语言中的写法就确定这样来写了。
VB9 的这个变化可以参看这篇博客:
Select/From vs. From/Select revisited...
我们再来看一个稍稍复杂的LINQ查询:
在我们列举的语言字符串中,我们希望按照字符长短,分类列举出来,实现代码如下:
static void Main(string[] args)
{
string[] languages = { Java, C#, C++, Delphi, VB.net, VC.net, C++Builder, Kylix, Perl, Python };
var query = from item in languages
group item by item.Length into lengthGroups
orderby lengthGroups.Key
select lengthGroups;
foreach (var item in query)
{
Console.WriteLine(strings of length{0}, item.Key);
foreach (var val in item)
{
Console.WriteLine(val);
}
}
Console.ReadLine();
}
其中的 into 关键字表示 将前一个查询的结果视为后续查询的生成器,这里是跟 group by 一起使用的。
LINQ中的Group by不要跟 SQL 中的Group by 混淆,SQL由于是二维结构,Group by 的一些逻辑受二维结构的约束,无法像 LINQ 中的Group by 这么灵活。
事实上,LINQ的查询语法存在以下两种形式:
查询方法方式:(Methord Syntax)
主要利用System.Linq.Enumerable类中定义的扩展方法和Lambda表达式方式进行查询
查询语句方式:(Query Syntax)一种更接近SQL语法的查询方式,可读性更好。