Tag: linq to entities

当生成的查询只需要1/2秒时,为什么Entity Framework需要30秒才能加载记录?

下面的executeTime是第一次30秒,下次执行同一组代码时是25秒。 在SQL事件探查器中观看时,我立即看到登录,然后它就在那里停留约30秒。 然后,只要运行select语句,应用程序就会完成ToList命令。 当我从Management Studio运行生成的查询时,数据库查询只需要大约400毫秒。 它返回14行和350列。 看起来像将数据库结果转换为实体所花费的时间非常小,不值得注意。 那么在数据库调用之前的30秒内发生了什么? 如果entity framework这么慢,我们就不可能使用它。 有什么我做错了或者我可以改变什么来加速这一点吗? 更新:好吧,如果我使用编译查询,第一次需要30秒,第二次需要1/4秒。 我有什么办法可以加快第一次通话的速度吗? using (EntitiesContext context = new EntitiesContext()) { Stopwatch sw = new Stopwatch(); sw.Start(); var groupQuery = (from g in context.Groups.Include(“DealContract”) .Include(“DealContract.Contracts”) .Include(“DealContract.Contracts.AdvertiserAccountType1”) .Include(“DealContract.Contracts.ContractItemDetails”) .Include(“DealContract.Contracts.Brands”) .Include(“DealContract.Contracts.Agencies”) .Include(“DealContract.Contracts.AdvertiserAccountType2”) .Include(“DealContract.Contracts.ContractProductLinks.Products”) .Include(“DealContract.Contracts.ContractPersonnelLinks”) .Include(“DealContract.Contracts.ContractSpotOrderTypes”) .Include(“DealContract.Contracts.Advertisers”) where g.GroupKey == 6 select g).OfType(); sw.Stop(); var queryTime = sw.Elapsed; sw.Reset(); […]

具有多个OR条件的Linq to Entity Join表

我需要编写一个可以获得以下SQL查询的Linq-Entity状态 SELECT RR.OrderId FROM dbo.TableOne RR JOIN dbo.TableTwo M ON RR.OrderedProductId = M.ProductID OR RR.SoldProductId= M.ProductID WHERE RR.StatusID IN ( 1, 4, 5, 6, 7 ) 我坚持使用以下语法 int[] statusIds = new int[] { 1, 4, 5, 6, 7 }; using (Entities context = new Entities()) { var query = (from RR in context.TableOne join M […]

将List 隐式转换为List

我正在使用Linq to Entities。 有一个实体“Order”,它有一个可为空的列“SplOrderID”。 我查询我的订单列表 List lst = Orders.where(u=> u.SplOrderID != null).Select(u => u.SplOrderID); 我理解这是因为SplOrderID是一个可以为空的列,因此select方法返回nullable int。 我只是期待LINQ有点聪明。 我该怎么处理?

在此上下文中仅支持基本类型(例如Int32,String和Guid’)

我收到以下错误: 无法创建类型为“Phoenix.Intranet.Web.ClientSettings.ComponentRole”的常量值。 在此上下文中仅支持基本类型(例如Int32,String和Guid’)。 我理解为什么会发生错误。 我不明白的是我的代码创建错误的原因。 我的比较是针对原始类型的。 所有的比较都是Guid to Guid。 该错误明确指出Guids是可以的。 此行发生错误(朝向底部): var vla = (from cir in phoenixEntities.ComponentInRoles 码: List roles; using (IMSMembershipEntities entities = new IMSMembershipEntities()) { roles = (from role1 in entities.Roles select new ComponentRole{Name = role1.RoleName, RoleId = role1.RoleId} ).ToList(); } List componentInRoles; using (PhoenixEntities phoenixEntities = new PhoenixEntities()) { phoenixEntities.ContextOptions.LazyLoadingEnabled = […]

entity framework中的条件包含()

我已经看到了类似问题的一些答案,但我似乎无法解决如何将答案应用于我的问题。 var allposts = _context.Posts .Include(p => p.Comments) .Include(aa => aa.Attachments) .Include(a => a.PostAuthor) .Where(t => t.PostAuthor.Id == postAuthorId).ToList(); 附件可以由作者(类型作者)或贡献者(类型贡献者)上传。 我想要做的是,只获取附件所有者属于作者类型的附件。 我知道这不起作用并给出错误: .Include(s=>aa.Attachments.Where(o=>o.Owner is Author)) 我在这里读过Filtered Projection 编辑 – 链接到文章:: http://blogs.msdn.com/b/alexj/archive/2009/10/13/tip-37-how-to-do-a-conditional-include.aspx , 但我无法理解它。 我不希望在最终的where子句中包含filter,因为我想要所有post,但我只想检索属于作者的那些post的附件。 编辑2: – 请求发布模式 public abstract class Post : IPostable { [Key] public int Id { get; set; } [Required] public DateTime […]

linq到实体的内部联接

我有一个名为Customer的实体,它有三个属性: public class Customer { public virtual Guid CompanyId; public virtual long Id; public virtual string Name; } 我还有一个名为Splitting的实体,它有三个属性: public class Splitting { public virtual long CustomerId; public virtual long Id; public virtual string Name; } 现在我需要编写一个获取companyId和customerId的方法。 该方法应返回与companyId中特定customerId相关的拆分列表。 像这样的东西: public IList get(Guid companyId, long customrId) { var res=from s in Splitting from c in Customer […]

EF Distinct(IEqualityComparer)错误

早上好! 鉴于: public class FooClass { public void FooMethod() { using (var myEntity = new MyEntity) { var result = myEntity.MyDomainEntity.Where(myDomainEntity => myDomainEntity.MySpecialID > default(int)).Distinct(new FooComparer); } } } public class FooComparer : IEqualityComparer { public bool Equals(MyEntity.MyDomainEntity x, MyEntity.MyDomainEntity y) { return x.MySpecialID == y.MySpecialID; } public int GetHashCode(MyEntity.MyDomainEntity obj) { return obj.MySpecialID.GetHashCode(); } […]

简单的自动化示例

我很难理解如何映射某些对象。 请回答一些关于这个简单示例的问题。 示例代码 class User { private int id; private string name; } class Group { private int id; private string name; private List users; } [DataContract] public class UserDto { [DataMember] public int id { get; set; } [DataMember] public string name{ get; set; } } [DataContract] public class GroupDto { [DataMember] public int […]

动态创建LINQ到实体OrderBy表达式

我正在尝试动态添加orderby表达式。 但是当执行下面的查询时,我得到以下exception: System.NotSupportedException:无法创建类型为“Closure type”的常量值。 在此上下文中仅支持基本类型(例如Int32,String和Guid’)。 奇怪的是,我只是查询那些原始类型。 string sortBy = HttpContext.Current.Request.QueryString[“sidx”]; ParameterExpression prm = Expression.Parameter(typeof(buskerPosting), “posting”); Expression orderByProperty = Expression.Property(prm, sortBy); // get the paged records IQueryable query = (from posting in be.buskerPosting where posting.buskerAccount.cmsMember.nodeId == m.Id orderby orderByProperty //orderby posting.Created select new PostingListItemDto { Set = posting }).Skip((page – 1) * pageSize).Take(pageSize); 希望有人能对此有所了解!

在Entity Framework 4中调用用户定义的函数

我在SQL Server 2005数据库中有一个用户定义的函数,它返回一个位。 我想通过entity framework调用此函数。 我一直在寻找并没有太多运气。 在LINQ to SQL中,这很简单,我只是将函数添加到Data上下文模型中,我可以像这样调用它。 bool result = FooContext.UserDefinedFunction(someParameter); 使用entity framework,我已将该函数添加到我的模型中,它出现在模型浏览器中的SomeModel.Store \ Stored Procedures下。 该模型没有为该函数生成代码,.edmx文件的XML包含: 我能得到的最接近的是这样的: bool result = ObjectContext.ExecuteFunction( “UserDefinedFunction”, new ObjectParameter(“someParameter”, someParameter) ).First(); 但是我收到以下错误消息: 无法在容器’FooEntities’中找到FunctionImport’UserDefinedFunction’。 名称已被更改以保护无辜者。 tldr:如何使用Entity Framework 4.0调用标量值的用户定义函数?