本文的目的是介绍“LINQtoEntities不支持LINQ表达式节点类型'Invoke'”-烦死了!的详细情况,我们将通过专业的研究、有关数据的分析等多种方式,同时也不会遗漏关于c#–linqexc
本文的目的是介绍“ LINQ to Entities不支持LINQ表达式节点类型'Invoke'”-烦死了!的详细情况,我们将通过专业的研究、有关数据的分析等多种方式,同时也不会遗漏关于c# – linq exception:只能从LINQ to Entities调用此函数、c# – LINQ to Entities DateTime不一致、c# – LINQ to Entities Skip and Take、c# – Linq to Entities Skip()和Take()的知识。
本文目录一览:- “ LINQ to Entities不支持LINQ表达式节点类型'Invoke'”-烦死了!
- c# – linq exception:只能从LINQ to Entities调用此函数
- c# – LINQ to Entities DateTime不一致
- c# – LINQ to Entities Skip and Take
- c# – Linq to Entities Skip()和Take()
“ LINQ to Entities不支持LINQ表达式节点类型'Invoke'”-烦死了!
稍后在我的EF中,我试图传递一个匿名函数以用作我的Linq查询的一部分。该函数将传入INT并返回BOOL(u.RelationTypeId为INT)。以下是我的函数的简化版本:
public IEnumerable<UserBandRelation> GetBandRelationsByUser(Func<int, bool> relation){ using (var ctx = new OpenGroovesEntities()) { Expression<Func<UsersBand, bool>> predicate = (u) => relation(u.RelationTypeId); var relations = ctx.UsersBands.Where(predicate); // mapping, other stuff, back to business layer return relations.ToList(); }}
但是,我得到上述错误。似乎我通过从函数构建谓词来使所有事情正确无误。有任何想法吗?谢谢。
答案1
小编典典您正在尝试在…中传递任意.NET函数。实体框架如何希望将其转换为SQL?您可以将其改为取一个Expression<Func<int,bool>>
,然后Where
从中构建子句,尽管这并不是 特别
容易,因为您需要使用其他参数表达式重写表达式(即替换原始表达式中的任何参数表达式)表示的树u.RelationTypeId
)。
老实说,为了仅u.RelationTypeId
在用于创建表达式树的lambda表达式中指定要传递到方法中,最好还是使用:
public IEnumerable<UserBandRelation> GetBandRelationsByUser( Expression<Func<UsersBand, bool>> predicate){ using (var ctx = new OpenGroovesEntities()) { var relations = ctx.UsersBands.Where(predicate); // mapping, other stuff, back to business layer return relations.ToList(); }}
c# – linq exception:只能从LINQ to Entities调用此函数
List<ExecutionLog3> reportserverDB = UpdateCache(); var reportLog = (from r in reportserverDB orderby r.TimeStart descending where ((model.reportName == null ? true : r.ItemPath.Contains(model.reportName)) && (model.reportFolder == null ? true : r.ItemPath.Contains(model.reportFolder)) && (r.TimeStart >= startDateTime) && (r.TimeStart <= endDateTime) ) select new FilteRSSrsLog { UserName = r.UserName,ReportName = r.ItemPath,ReportFolder = r.ItemPath,Format = r.Format,Parameters = r.Parameters,TimeStart = r.TimeStart,TimeEnd = r.TimeEnd,TotalTime = EntityFunctions.DiffMilliseconds(r.TimeStart,r.TimeEnd) });
如果我删除“选择新的FilteRSSrsLog”代码块并写入“select r”它可以工作.但我只需要那些颜色,那么我该怎么做才能解决这个问题呢?
解决方法
如果要在内存中运行此查询,请替换
TotalTime = EntityFunctions.DiffMilliseconds(r.TimeStart,r.TimeEnd)
同
TotalTime = (r.TimeEnd - r.TimeStart).TotalMilliseconds
减去两个日期会产生一个TimeSpan
值,您可以从中获取其TotalMilliseconds
属性.
c# – LINQ to Entities DateTime不一致
DateTime date = DateTime.Now; DateTest newRecord = new DateTest { dateColumn = date }; db.DateTest.Addobject(newRecord); db.SaveChanges(); IQueryable<DateTest> records = from d in db.DateTest select d;
如果我此时破坏代码,并查看调试器中的对象,我得到日期对象:
Date {11/22/2011 12:00:00 AM} System.DateTime Day 22 int DayOfWeek Tuesday System.DayOfWeek DayOfYear 326 int Hour 8 int Kind Local System.DateTimeKind Millisecond 345 int Minute 59 int Month 11 int Second 33 int Ticks 634575491733450602 long TimeOfDay {08:59:33.3450602} System.TimeSpan Year 2011 int
我从表中检索到的记录得到了这个:
Date {11/22/2011 12:00:00 AM} System.DateTime Day 22 int DayOfWeek Tuesday System.DayOfWeek DayOfYear 326 int Hour 8 int Kind Unspecified System.DateTimeKind Millisecond 347 int Minute 59 int Month 11 int Second 33 int Ticks 634575491733470000 long TimeOfDay {08:59:33.3470000} System.TimeSpan Year 2011 int
正如你所看到的,它们会在几毫秒内完成.
任何人都可以解释为什么这样,以及我如何解决它?我需要能够查询与内存中的DateTime对象完全匹配的记录,但是这种行为导致我的查询空手而归.
解决方法
来自MSDN – datetime (Transact-SQL):
Accuracy: Rounded to increments of .000,.003,or .007 seconds
因此,在您的情况下,毫秒会向上舍入到.007,给出.3470000而不是.3450602.
DateTime2
sql Server数据类型的分辨率为100纳秒,与.NET类似,因此可能是合适的替代品.
c# – LINQ to Entities Skip and Take
在这个方法中我使用Linq to Entities来加载页面的元素:
public List<Item> GetElements(int page,int numberOfElementsPerPage) { return DataContext.Items.OrderBy(x => x.Id).Skip((page-1)*numberOfElementsPerPage).Take(numberOfElementsPerPage); }
我想问一下,这个Skip / Take是如何工作的?它是否首先从数据库,订单和Skip / Take获取所有记录?如果是,我认为如果数据库获得100000条记录甚至更多,这是非常糟糕的解决方案.那么最好的解决方案是什么?
解决方法
So whats the best solution?
这是最好的解决方案.
If yes I think this is pretty bad solution
你是对的,如果以这种方式实施,那将是一个非常糟糕的解决方案.幸运的是,它没有以这种方式实现:Skip和Take的值以特定于sql方言的方式传递给您的RDBMS服务器,此时数据库决定如何查找和为您提供记录.对于sql Server,使用与此类似的语法:
SELECT ... FROM ... WHERE ... ... OFFSET <skip-value> ROWS FETCH NEXT <take-value> ROWS ONLY;
c# – Linq to Entities Skip()和Take()
>如何增加Skip(),Take()参数来查看下一个结果?
>如何使用“IN”关键字,如果用户从列表框中选择多个选项,查询可以检查所有值?
我的查询如下所示:
var searchResults = context.data_vault.Where(d => d.STATE == lstStates.SelectedItem.Text).OrderBy(d= > d.dv_id).Take(10).Skip(2); GridView1.DataSource = searchResults; GridView1.DataBind();
解决方法
Skip跳过一些记录,所以在第一页,传入0,否则传入每页的(页码 – 1)*记录.
我通常做这样的事情:
int Page = 1; int RecordsPerPage = 10; var q = yourQuery.Skip((Page - 1) * RecordsPerPage).Take(RecordsPerPage);
关于“ LINQ to Entities不支持LINQ表达式节点类型'Invoke'”-烦死了!的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于c# – linq exception:只能从LINQ to Entities调用此函数、c# – LINQ to Entities DateTime不一致、c# – LINQ to Entities Skip and Take、c# – Linq to Entities Skip()和Take()等相关知识的信息别忘了在本站进行查找喔。
本文标签: