string
esql =
"select value c from NorthwindEntities.Customers as c order by
c.CustomerID limit 10"
;
ObjectQuery
<
Customers
> query = edm.CreateQuery<
Customers
>(esql);
Console
.WriteLine(query.GetResultType().ToString());
//
输出结果为:
//NorthWindModel.Customers
}
ToTraceString
方法:
获取当前执行的
SQL
语句。
Where
实例代码如下:
using
(
var
edm =
new
NorthwindEntities
())
{
string
esql =
"select value c from NorthwindEntities.Customers as c "
;
ObjectQuery
<
Customers
> query1 = edm.CreateQuery<
Customers
>(esql);
//
使用
ObjectParameter
的写法
query1 = query1.Where(
"it.CustomerId=@customerid"
);
query1.Parameters.Add(
new
ObjectParameter
(
"customerid"
,
"ALFKI"
));
//
也可以这样写
//ObjectQuery<Customers> query2 = edm.Customers.Where("it.CustomerID='ALFKI'");
foreach
(
var
c
in
query1)
Console
.WriteLine(c.CustomerID);
//
显示查询执行的
SQL
语句
Console
.WriteLine(query1.ToTraceString());
}
First/ FirstOrDefault
实例代码如下:
using
(
var
edm =
new
NorthwindEntities
())
{
string
esql =
"select value c from NorthwindEntities.Customers as c order by
c.CustomerID limit 10"
;
ObjectQuery
<
Customers
> query = edm.CreateQuery<
Customers
>(esql);
Customers
c1 = query.First();
Customers
c2 = query.FirstOrDefault();
Console
.WriteLine(c1.CustomerID);
Assert
.IsNotNull(c2);
Console
.WriteLine(c2.CustomerID);
}
Distinct
实例代码如下:
using
(
var
edm =
new
NorthwindEntities
())
{
string
esql =
"select value c.City from NorthwindEntities.Customers as c order by
c.CustomerID limit 10"
;
ObjectQuery
<
string
> query = edm.CreateQuery<
string
>(esql);
query = query.Distinct();
foreach
(
string
c
in
query)
{
Console
.WriteLine(
"City {0}"
, c);
}
}
Except:
返回两个查询的差集。实例代码如下:
using
(
var
edm =
new
NorthwindEntities
())
{
string
esql1 =
"select value c from NorthwindEntities.Customers as c order by
c.CustomerID limit 10"
;
ObjectQuery
<
Customers
> query1 = edm.CreateQuery<
Customers
>(esql1);
string
esql2 =
"select value c from NorthwindEntities.Customers as c where
c.Country='UK' order by c.CustomerID limit 10"
;
ObjectQuery
<
Customers
> query2 = edm.CreateQuery<
Customers
>(esql2);
query1 = query1.Except(query2);