我有这个:
var points = from p in ContextDB.Points
orderby p.PointInTime descending
where p.InstanceID == instanceId
&& p.ParentPointID == null
&& p.PointTypeID == currentPointTypeID
select p;
Run Code Online (Sandbox Code Playgroud)
还有这个:
var points = from p in ContextDB.Points
where p.InstanceID == instanceId
&& p.ParentPointID == null
&& p.PointTypeID == currentPointTypeID
orderby p.PointInTime descending
select p;
Run Code Online (Sandbox Code Playgroud)
虽然我理解两者的使用(以及之后会产生错误)但我不明白它们是如何不同的.
我确实在STO的其他地方看到过这样的问题,但是我没有意识到这个问题的答案是什么,我很害怕.
将LINQ字符串解析为查询的最佳实践方法是什么?
换句话说,转换最有意义的方法是:
string query = @"from element in source
where element.Property = ""param""
select element";
Run Code Online (Sandbox Code Playgroud)
成
IEnumerable<Element> = from element in source
where element.Property = "param"
select element;
Run Code Online (Sandbox Code Playgroud)
假设source指的是IEnumerable<Element>或IQueryable<Element>在本地范围内.
如果我有以下代码: -
IQueryable<People> list = repository.FindAllPeople;
int count = list.Count();
Run Code Online (Sandbox Code Playgroud)
然后认为计算IQueryable对象是不可行的,使用IEnumerable更好吗?BR
在LINQ to SQL类,为什么属性是从外键创建EntitySet对象,实施IEnumerable,作为地方上的对象DataContext是Table其实现目标IQueryable?
编辑:澄清一下,这是一个例子,说明了我想要了解的内容.这个例子:
ctx.Matches.Where(x => x.MatchID == 1).Single()
.MatchPlayers.Max(x => x.Score);
Run Code Online (Sandbox Code Playgroud)
在以下位置点击数据库两次:
ctx.MatchPlayers.Where(x => x.MatchID == 1)
.Max(x => x.Score);
Run Code Online (Sandbox Code Playgroud)
只运行1个查询.以下是痕迹:
exec sp_executesql N'SELECT [t0].[MatchID], [t0].[Date]
FROM [dbo].[Matches] AS [t0]
WHERE [t0].[MatchID] = @p0',N'@p0 int',@p0=1
go
exec sp_executesql N'SELECT [t0].[MatchID], [t0].[PlayerID], [t0].[Score]
FROM [dbo].[MatchPlayers] AS [t0]
WHERE [t0].[MatchID] = @p0',N'@p0 int',@p0=1
go
Run Code Online (Sandbox Code Playgroud)
和
exec sp_executesql N'SELECT MAX([t0].[Score]) AS [value]
FROM [dbo].[MatchPlayers] AS [t0]
WHERE [t0].[MatchID] = @p0',N'@p0 int',@p0=1 …Run Code Online (Sandbox Code Playgroud) 我想IQueryable<>在执行存储过程时获得结果.
这是代码的和平工作正常:
IQueryable<SomeEntitiy> someEntities;
var globbalyFilteredSomeEntities =
from se in m_Entities.SomeEntitiy
where
se.GlobalFilter == 1234
select se;
Run Code Online (Sandbox Code Playgroud)
我可以使用它来应用全局过滤器,然后以这种方式使用结果
result = globbalyFilteredSomeEntities
.OrderByDescending(se => se.CreationDate)
.Skip(500)
.Take(10);
Run Code Online (Sandbox Code Playgroud)
我想做什么 - 在全局过滤器中使用一些存储过程.
我试过了:
添加存储过程m_Entities,但它会IEnumerable<>立即返回并执行sp:
var globbalyFilteredSomeEntities =
from se in m_Entities.SomeEntitiyStoredProcedure(1234);
Run Code Online (Sandbox Code Playgroud)
使用EFExtensions库实现查询,但确实如此IEnumerable<>.
如果我使用AsQueryable()和OrderBy(),Skip(),Take()
,之后ToList()执行该查询-
我得到异常DataReader是开放的,我需要先关闭它(不能粘贴错误-这是俄语).
var globbalyFilteredSomeEntities =
m_Entities.CreateStoreCommand("exec SomeEntitiyStoredProcedure(1234)")
.Materialize<SomeEntitiy>();
//.AsQueryable()
//.OrderByDescending(se => se.CreationDate)
//.Skip(500)
//.Take(10)
//.ToList();
Run Code Online (Sandbox Code Playgroud)
也只是跳过.AsQueryable()没有帮助 - 同样的例外.
当我 …
.net linq-to-entities stored-procedures entity-framework iqueryable
我有一个名为ICatalog如下所示的接口,其中每个接口ICatalog都有一个名称和一个基于Predicate<Item>函数返回项目的方法.
public interface ICatalog
{
string Name { get; }
IEnumerable<Item> GetItems(Predicate<Item> predicate);
}
Run Code Online (Sandbox Code Playgroud)
目录的特定实现可以链接到各种格式的目录,例如XML或SQL数据库.
使用XML目录,我最终将整个XML文件反序列化为内存,因此使用谓词函数测试每个项目并不会增加更多的开销,因为它已经在内存中.
然而,在SQL实现中,我宁愿不将数据库的全部内容检索到内存中,然后使用谓词函数过滤项目.相反,我想找到一种方法以某种方式将谓词传递给SQL服务器,或以某种方式将其转换为SQL查询.
这似乎是一个可以用Linq解决的问题,但我对它很陌生.我的界面应该返回IQueryable吗?我现在不关心如何实际实现我的ICatalog的SQL版本.我只是想确保我的界面将来允许它.
我有一个A具有简单导航属性的实体B.对于任何给定的实例A,我们期望几个相关的千个实例B.
没有我称之为的情况:
foreach(var x in A.B) { ... }
Run Code Online (Sandbox Code Playgroud)
相反,我只对进行聚合操作感兴趣
var statY = A.B.Where(o => o.Property == "Y");
var statZ = A.B.Where(o => o.CreateDate > DateTime.Now.AddDays(-1));
Run Code Online (Sandbox Code Playgroud)
据我所知,EF实例化了数千个对B的引用,并在内存中执行这些操作.这是因为导航属性使用EntityCollection.相反,我希望它尽可能在SQL级别执行这些查询.
我目前的预感是导航属性可能不是正确的方法.我不喜欢EF,所以我对其他方法持开放态度.但是如果可能的话,我很想知道在EF下正确的方法.
(我正在使用EF4.)
我使用EntityFramework,我正在查询并使用匿名类型返回部分数据.目前我正在使用IQueryable<dynamic>,它的工作原理,但我想知道这是否是正确的方法,或者是否有其他一些我不知道的返回数据类型.
public IQueryable<dynamic> FindUpcomingEventsCustom(int daysFuture)
{
DateTime dateTimeNow = DateTime.UtcNow;
DateTime dateTimeFuture = dateTimeNow.AddDays(daysFuture);
return db.EventCustoms.Where(x => x.DataTimeStart > dateTimeNow & x.DataTimeStart <= dateTimeFuture)
.Select(y => new { y.EventId, y.EventTitle, y.DataTimeStart});
}
Run Code Online (Sandbox Code Playgroud) 我正在阅读一篇关于Linq to Sql的文章并且发现了这个:
IQueryProvider provider = new QueryProvider(database.GetCommand, database.ExecuteQuery);
IQueryable<Product> source = new Queryable<Product>(provider, database.GetTable<Product>());
IQueryable<string> results = source.Where(product => product.CategoryID == 2)
.OrderBy(product => product.ProductName)
.Select(product => product.ProductName)
.Skip(5)
.Take(10);
Run Code Online (Sandbox Code Playgroud)
然后作者将结果翻译成普通的sql:
exec sp_executesql N'SELECT [t1].[ProductName]
FROM (
SELECT ROW_NUMBER() OVER (ORDER BY [t0].[ProductName]) AS [ROW_NUMBER], [t0].[ProductName]
FROM [dbo].[Products] AS [t0]
WHERE [t0].[CategoryID] > @p0
) AS [t1]
WHERE [t1].[ROW_NUMBER] BETWEEN @p1 + 1 AND @p1 + @p2
ORDER BY [t1].[ROW_NUMBER]',N'@p0 int,@p1 int,@p2 int',@p0=2,@p1=5,@p2=10
Run Code Online (Sandbox Code Playgroud)
而且我心里想,"神圣的牛!如果有一个IQueryable扩展可以在调试时为你生成这些字符串,那会不会很好?"
任何人都听说过这样的事情,如果有的话,你能指出我正确的方向吗?
谢谢!
我一直在和这个摔跤,现在看起来似乎不太可能.
我想要Concat()两个IQueryable,然后将结果作为单个查询执行.我试过这样的事情:
var query = from x in ...
select new
{
A = ...
B = ...
C = ...
};
var query2 = from y in ...
select new
{
A = ...
B = ...
C = ...
};
var query3 = query.Concat(query2);
Run Code Online (Sandbox Code Playgroud)
但是,最后一行给出了以下错误:
'System.Linq.IQueryable'不包含'Concat'的定义,并且最好的扩展方法重载'System.Linq.ParallelEnumerable.Concat(System.Linq.ParallelQuery,System.Collections.Generic.IEnumerable)'有一些无效的参数
看起来它正在期待IEnumerable争论.有没有办法解决?
看起来我可以将两个查询解析为IEnumerables然后再解决Concat()它们.但是创建单个查询会更有效,而且看起来应该是可行的.