简体   繁体   English

无法创建“匿名类型”类型的常量值。 在此上下文中仅支持基元类型或枚举类型

[英]Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context

I am extremely new to linq and entity framework. 我对linq和实体框架非常linq I am trying to resolve a problem as to why the below isn't working. 我试图解决一个问题,为什么以下不起作用。 The error produced is "Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context." 产生的错误是“无法创建类型为'匿名类型'的常量值。在此上下文中仅支持基本类型或枚举类型。”

I've tried this so many different ways yet still get an error relating to primitive types. 我尝试了这么多不同的方法,但仍然得到与原始类型有关的错误。 I would greatly appreciate it if someone could take a look at the below code and hopefully point out where it's going wrong. 如果有人能够看一下下面的代码并希望指出哪里出错,我将不胜感激。

        public Entities.BikeData[] GetBikesWithExpiredSyncDeadline(int noOfDays) {

        using (OfficeEntities cxt = GetContext()) 
        using (ReportingEntities RepCxt = GetReportingContext()) {
            Data.Repository.Abstract.IBikeRepository BikeRepos = new Data.Repository.Concrete.BikeRepository();                

            var details = (from sd in cxt.BikeDetails
                                        where sd.autoreminder == true
                                            && (sd.lastremindersent == null || sd.lastremindersent < EntityFunctions.AddDays(DateTime.UtcNow, noOfDays * -1))
                                            && (sd.emailaddress != null && sd.emailaddress.Trim() != "")
                                        select new {
                                            Serial = sd.Serial,
                                            EmailAddress = sd.emailaddress
                                        }).ToList();

            var resLst = (from r in RepCxt.RegisteredBikes
                          join d in details on r.Serial equals d.Serial 
                          join cs in cxt.CompanySettings.ToList() on r.CompanyID equals cs.CompanyID
                          where (!r.lastupdate.HasValue || r.lastupdate < EntityFunctions.AddDays(DateTime.UtcNow, cs.AutoNotificationFrequency * -1))
                          select new Entities.BikeData {
                              ID = r.ID,
                              Name = r.Ship,
                              Serial = r.Serial,
                              LastUpdate = r.lastupdate,
                              DaysSinceLastSync = (r.lastupdate.HasValue? EntityFunctions.DiffDays(r.lastupdate.Value, DateTime.UtcNow).Value : -1),
                              EmailAddress = (d.EmailAddress == null ? string.Empty : (String.IsNullOrEmpty(d.EmailAddress) ? r.ShipEmailAddress : d.EmailAddress))
                          });

            return resLst.ToArray();
        }
    }

UPDATE UPDATE

I've taken a different approach with this now by creating a view so I no longer need to do the cross context joins in EF. 我现在通过创建视图采用了不同的方法,因此我不再需要在EF中进行交叉上下文连接。 I was hoping you may be able to help with the below. 我希望你能帮助解决下面这个问题。

When I run objectQuery.ToTraceString() it provides me with valid SQL that returns records in the db, however the resLst in EntityFramework always comes back with 0. Is there anything obvious as to why this is happening? 当我运行objectQuery.ToTraceString()时,它为我提供了返回db中记录的有效SQL,但是EntityFramework中的resLst总是返回0.是否有任何明显的原因发生?

  var resLst = (from ls in cxt.BikeLastUpdates
                          where (!ls.lastupdate.HasValue || ls.lastupdate < EntityFunctions.AddDays(DateTime.UtcNow, ls.AutoNotificationFrequency * -1))
                          && (ls.autoreminder ==true)
                          && (ls.lastremindersent == null || ls.lastremindersent < EntityFunctions.AddDays(DateTime.UtcNow, 3 * -1))
                          && (ls.emailaddress !=null && ls.emailaddress.Trim() != "")
                          select new Entities.BikeData{
                              ID = (ls.ID ?? new Guid()),
                              Name = ls.Bike,
                              Serial = ls.Serial,
                              LastUpdate = ls.lastupdate,
                              EmailAddress = (String.IsNullOrEmpty(ls.emailaddress) ?  ls.ShipEmailAddress : ls.emailaddress)
                          });

            var objectQuery = resLst as ObjectQuery;

            return resLst.ToArray();

The problem is the ToList() call on details. 问题是ToList()调用细节。 In EF, you can only refer to an IEnumerable inside a Query if that IEnumerable is of a simple type (eg int). 在EF中,如果IEnumerable是一个简单类型(例如int),则只能引用Query中的IEnumerable。 However, you CAN refer to another IQueryable. 但是,您可以引用另一个IQueryable。 Thus, dropping the ToList() call should make this work. 因此,删除ToList()调用应该可以使这个工作。

EDIT: similarly, you should drop the ToList() call on ctx.CompanySettings. 编辑:同样,你应该删除ctx.CompanySettings上的ToList()调用。

This will have the added advantage of executing only 1 query instead of 2. If you drop the ToList() on details, EF will generate something like: 这将具有仅执行1个查询而不是2的额外优势。如果在详细信息上删除ToList(),EF将生成如下内容:

SELECT ...
FROM RegisteredBikes rb
JOIN (
    /* this is your "details" IQueryable */
    SELECT Serial, EmailAddress
    FROM BikeDetails
    WHERE ...
) bd
    ON rb.Serial = b.Serial
JOIN CompanySettings cs
    ON ...
WHERE ...

EDIT: to do this across contexts, you'll need to bring the query into memory (eg by calling AsEnumerable() and do the relevant joins there. If the joins act as filters and it's important for these to happen in SQL, consider using Contains(). For example 编辑:要跨上下文执行此操作,您需要将查询带入内存(例如,通过调用AsEnumerable()并在那里执行相关联接。如果联接充当过滤器并且在SQL中发生这些事件很重要,请考虑使用包含()。例如

var serials = details.Select(d => d.Serial);
var filtered = RepCtxt.RegisteredBikes.Where(r => details.Contains(r.Serial);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EntityFramework无法创建“匿名类型”类型的常量值。 在此上下文中仅支持基元类型或枚举类型 - EntityFramework Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context EF错误无法创建类型为“匿名类型”的常量值。 在此上下文中仅支持原始类型或枚举类型 - EF error Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context 无法创建类型为“匿名类型”的常量值。 在此上下文中仅支持原始类型或枚举类型两个db Linq查询 - Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context two db Linq query 无法创建类型为“匿名类型”的常量值。 在此上下文中仅支持原始类型或枚举类型。 在Linq C#中 - Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context. in Linq C# UOW模式-无法创建类型的常量值在此上下文中仅支持基本类型或枚举类型 - UOW pattern - Unable to create a constant value of type Only primitive types or enumeration types are supported in this context C# - Linq:无法创建类型的常量值在此上下文中仅支持基本类型或枚举类型。 - C# - Linq : Unable to create a constant value of type Only primitive types or enumeration types are supported in this context. LINQ,无法创建XXX类型的常量值。在此上下文中仅支持基元类型或枚举类型 - LINQ, Unable to create a constant value of type XXX. Only primitive types or enumeration types are supported in this context 错误:无法创建类型为“ System.Object”的常量值。 在此上下文中仅支持原始类型或枚举类型 - Error: Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context LINQ查询错误:无法创建类型的常量值。 在此上下文中仅支持原始类型或枚举类型 - LINQ Query error: Unable to create a constant value of type. Only primitive types or enumeration types are supported in this context 错误:无法创建类型的常量值。 在此上下文中仅支持原始类型或枚举类型 - Error: Unable to create a constant value of type . Only primitive types or enumeration types are supported in this context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM