Avg
平均值 |
|
BigCount
个数(long) |
|
Count
个数(int) |
|
Max
最大值 |
|
Min
最小值 |
|
Sum
合计 |
|
联合使用
myContext context = new myContext();
string esql = "SELECT Max(it.ItemValue) as Max , Min(it.ItemValue) as Min FROM myContext.DBItemList as it";
ObjectQuery<DbDataRecord> query = context.CreateQuery<DbDataRecord>(esql);
foreach (DbDataRecord r in query)
{
Console.WriteLine("Max:{0},Min:{1}", r["Max"], r["Min"]);
}
/* print:
Max:6,Min:1
*/
与group by一起使用
myContext context = new myContext();
string esql = "SELECT ID as ItemID , Max(it.ItemValue) as Max , Min(it.ItemValue) as Min FROM myContext.DBItemList as it group by it.ItemID as ID";
ObjectQuery<DbDataRecord> query = context.CreateQuery<DbDataRecord>(esql);
foreach (DbDataRecord r in query)
{
Console.WriteLine("ItemID:{0}, Max:{1},Min:{2}", r["ItemID"], r["Max"], r["Min"]);
}
/* print:
ItemID:a, Max:6,Min:2
ItemID:b, Max:5,Min:1
ItemID:c, Max:6,Min:2
*/
数学类
Abs
绝对值 |
Abs(-2) |
Round
随机数 |
Round(748.58) |
日期
CurrentDateTime() | |
CurrentDateTimeOffset() | |
CurrentUtcDateTime() | |
Day( expression ) | Day(cast('03/12/1998' as DateTime)) --返回:12 |
GetTotalOffsetMinutes | --返回:
SQL Server 2008 only |
Hour ( expression ) | Hour(cast('22:35:5' as DateTime)) --返回:22 |
Minute( expression ) | Minute(cast('22:35:5' as DateTime)) --返回:35 |
Month (expression) | Month(cast('03/12/1998' as DateTime)) --返回:3 |
Second( expression ) | Second(cast('22:35:5' as DateTime)) --返回:5 |
Year( expression ) | Year(cast('03/12/1998' as DateTime)) --返回:1998 |
字符
Concat ( string1, string2)
字符串连接 |
Concat('abc', 'xyz') --返回:abcxyz |
IndexOf( string1, string2)
字符串位置查找 |
IndexOf('xyz', 'abcxyz') --返回:4 |
Length ( string )
字符串长度 |
Legth('abcxyz') --返回:6 |
Reverse ( string )
字符串反转 |
Reverse('abcd') --返回:dcba |
ToLower( string )
大写转小写 |
ToLower('ABC') --返回:abc |
ToUpper( string )
小写转大写 |
ToUpper('abc') --返回:ABC |
Trim( string )
去两端空格 |
Trim(' abc ') --返回:abc |
LTrim( string )
去左端空格 |
LTrim(' abc') --返回:abc |
RTrim( string )
去右端空格 |
|
Left ( string, length)
左端截取 |
Left('abcxyz', 3) --返回:abc |
Right ( string, length)
右端截取 |
Right('abcxyz', 3) --返回:xyz |
Substring ( string, start, length)
两端截取 |
Substring('abcxyz', 4, 3) --返回:xyz |
esql语句 查询语句
SELECT |
|
|
|
WHERE |
|
|
|
GROUP BY |
|
|
|
ORDER BY |
|
|
|
HAVING |
|
|
|
JOIN |
|
CASE语句
CASE WHEN THEN ELSE END
myContext context = new myContext();
string esql = "select it.ItemID, it.ItemValue ,(Case when it.ItemValue =1 then '差' when it.ItemValue between 2 and 4 then '好' else '其他' end) as ItemValueRemarks from myContext.DBItemList as it";
ObjectQuery<DbDataRecord> query = context.CreateQuery<DbDataRecord>(esql);
foreach (DbDataRecord r in query)
{
Console.WriteLine("{0},{1},{2}", r["ItemID"], r["ItemValue"], r["ItemValueRemarks"]);
}
/*
c,4,好
c,5,其他
c,2,好
c,3,好
b,5,其他
c,6,其他
b,2,好
b,1,差
c,3,好
a,4,好
a,5,其他
a,2,好
a,3,好
a,6,其他
a,3,好
*/
esql 类型 简单类型
Null |
|
|
|
Boolean |
|
|
|
Integer Float,Double Decimal |
|
|
|
String |
|
|
|
DateTime |
|
|
|
Time |
|
DateTimeOffset |
|
Binary |
|
Guid |
|
|
REF
myContext context = new myContext();
//string esql = "SELECT it.ItemID FROM DBItem as it";
string esql = "SELECT REF(it).ItemID FROM DBItem as it";
ObjectQuery<DbDataRecord> v = context.CreateQuery<DbDataRecord>(esql);
foreach (DbDataRecord r in v)
{
Console.WriteLine("{0}", r[0]);
}
ROW
myContext context = new myContext();
string esql = "select value row( it.ItemValue ,it.NameID,'wxd' as wxwinter) from myContext.DBItemList as it";
ObjectQuery<DbDataRecord> query = context.CreateQuery<DbDataRecord>(esql);
foreach (DbDataRecord r in query)
{
Console.WriteLine("{0},{1},{2}", r["ItemValue"], r["NameID"], r["wxwinter"]);
}
myContext context = new myContext();
string esql = "select row( it.ItemValue ,it.NameID) as wxd ,it.ItemID from myContext.DBItemList as it";
ObjectQuery<DbDataRecord> query = context.CreateQuery<DbDataRecord>(esql);
foreach (DbDataRecord r in query)
{
DbDataRecord v = r["wxd"] as DbDataRecord;
Console.WriteLine("{0},{1},{2}", r["ItemID"],v["ItemValue"],v["NameID"]);
}
集合
MULTISET(1,2,3,4)
{1,2,3,4}
SELECT VALUE it FROM [DBItemList] AS it WHERE it.ItemValue IN {1,2,3}
myContext context = new myContext();
string esql = "{1,2,3} ";
ObjectQuery<int> query = context.CreateQuery<int>(esql);
foreach (int r in query)
{
System.Console.WriteLine(r);
}
SELECT
[UnionAll2].[C1] AS [C1]
FROM (SELECT
[UnionAll1].[C1] AS [C1]
FROM (SELECT
1 AS [C1]
FROM ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable1]
UNION ALL
SELECT
2 AS [C1]
FROM ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable2]) AS [UnionAll1]
UNION ALL
SELECT
3 AS [C1]
FROM ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable3]) AS [UnionAll2]
myContext context = new myContext();
string esql = "{row(1 as a,'wxd' as wxwinter),row(2 as a,'lzm' as wxwinter),row(3 as a,'wxwinter' as wxwinter)} ";
ObjectQuery<DbDataRecord> query = context.CreateQuery<DbDataRecord>(esql);
foreach (DbDataRecord r in query)
{
System.Console.WriteLine("{0},{1}",r["a"],r["wxwinter"]);
}
/*
1,wxd
2,lzm
3,wxwinter
*/
SELECT
1 AS [C1],
CASE WHEN ([UnionAll2].[C1] = 0) THEN 1 WHEN ([UnionAll2].[C1] = 1) THEN 2 ELSE 3 END AS [C2],
CASE WHEN ([UnionAll2].[C1] = 0) THEN 'wxd' WHEN ([UnionAll2].[C1] = 1) THEN 'lzm' ELSE 'wxwinter' END AS [C3]
FROM (SELECT
[UnionAll1].[C1] AS [C1]
FROM (SELECT
0 AS [C1]
FROM ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable1]
UNION ALL
SELECT
1 AS [C1]
FROM ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable2]) AS [UnionAll1]
UNION ALL
SELECT
2 AS [C1]
FROM ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable3]) AS [UnionAll2]
Object 返回对像
Select 选出的值可以直接创建为对像后,将对像放入字段中返回
myContext context = new myContext();
string esql = "SELECT [WindowsFormsApplication8].[DBItemEx](it.ItemID + 'b') as myObject FROM DBItem as it";
ObjectQuery<DbDataRecord> v = context.CreateQuery<DbDataRecord>(esql);
foreach (DbDataRecord r in v)
{
DBItemEx obj = r["myObject"] as DBItemEx;
Console.WriteLine("{0}", obj.ItemID);
}
CAST 类型转换
myContext context = new myContext();
string esql = "select value CAST(it.ItemValue as System.String) from myContext.DBItemList as it";
ObjectQuery<string> query = context.CreateQuery<string>(esql);
foreach (string r in query)
{
Console.WriteLine(r);
}
myContext context = new myContext();
string esql = "using System;select value CAST(it.ItemValue as String) from myContext.DBItemList as it";
ObjectQuery<string> query = context.CreateQuery<string>(esql);
foreach (string r in query)
{
Console.WriteLine(r);
}
CAST( d as Edm.Decimal(16, 2) )
OFTYPE
OFTYPE ( expression, [ONLY] test_type )
myContext context = new myContext();
string esql = "OFTYPE(((SELECT VALUE it FROM DBItem as it) ),[WindowsFormsApplication8].[DBItemEx])";
ObjectQuery<DBItemEx> v = context.CreateQuery<DBItemEx>(esql);
与如下效果相同
myContext context = new myContext();
ObjectQuery<DBItemEx> v = context.DBItem.OfType<DBItemEx>();
TREAT
myContext context = new myContext();
string esql = "SELECT TREAT(it AS [WindowsFormsApplication8].[DBItemEx]) FROM DBItem AS it";
ObjectQuery<DbDataRecord> v = context.CreateQuery<DbDataRecord>(esql);
foreach (DbDataRecord r in v)
{
DBItemEx obj = r[0] as DBItemEx;
if (obj != null)
{
Console.WriteLine("{0}", obj.ItemID);
}
}
与如下效果类似
myContext context = new myContext();
ObjectQuery<DBItemEx> v = context.DBItem.OfType<DBItemEx>();
IS 类型判断
myContext context = new myContext();
string esql = "SELECT TREAT(it AS [WindowsFormsApplication8].[DBItemEx]) FROM DBItem AS it WHERE it IS OF ([WindowsFormsApplication8].[DBItemEx])";
ObjectQuery<DbDataRecord> v = context.CreateQuery<DbDataRecord>(esql);
foreach (DbDataRecord r in v)
{
DBItemEx obj = r[0] as DBItemEx;
// if (obj != null)
{
Console.WriteLine("{0}", obj.ItemID);
}
}
与如下效果类似
myContext context = new myContext();
ObjectQuery<DBItemEx> v = context.DBItem.OfType<DBItemEx>();
esql Namespace 使用SqlServer函数
using SqlServer; |
|
SqlServer.函数 |
|
|
使用NET的数据类型
myContext context = new myContext();
string esql = "using System;select value CAST(it.ItemValue as String) from myContext.DBItemList as it";
ObjectQuery<string> query = context.CreateQuery<string>(esql);
foreach (string r in query)
{
Console.WriteLine(r);
}
myContext context = new myContext();
// ObjectQuery<typeTest> query = context.typeTest.Where("it.d==DATETIME'1977-11-11 00:00:00.000'");
ObjectQuery<typeTest> query = context.typeTest.Where("it.d==cast('1977-11-11' as System.DateTime)");
foreach (typeTest r in query)
{
Console.WriteLine("{0},{1},{2},{3},{4},{5}",r.a,r.b,r.c,r.d,r.e,r.值);
}
esql关系,导航 KEY
myContext context = new myContext();
string esql="SELECT VALUE [TargetEntity] FROM (SELECT VALUE x FROM [myContext].[FK_DBItemList_DBItem] AS x WHERE Key(x.[DBItem]) = ROW(@EntityKeyValue1 AS EntityKeyValue1)) AS [AssociationEntry] INNER JOIN [myContext].[DBItemList] AS [TargetEntity] ON Key([AssociationEntry].[DBItemList]) = Key(Ref([TargetEntity]))";
ObjectQuery<DBItemList> dbitemlist = context.CreateQuery<DBItemList>(esql, new ObjectParameter("EntityKeyValue1", "a"));
foreach (DBItemList r in dbitemlist)
{
Console.WriteLine("{0},{1},{2}", r.AutoId, r.ItemValue, r.NameID);
}
效果同下
myContext context = new myContext();
ObjectQuery<DBItemList> dbitemlist = context.DBItem.First(p => p.ItemID == "a").DBItemList.CreateSourceQuery();
foreach (DBItemList r in dbitemlist)
{
Console.WriteLine("{0},{1},{2}", r.AutoId, r.ItemValue, r.NameID);
}