Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我只需要强制转换基元类型?_C#_Entity Framework - Fatal编程技术网

C# 我只需要强制转换基元类型?

C# 我只需要强制转换基元类型?,c#,entity-framework,C#,Entity Framework,我正试图在实体框架中使用匿名类型,但我得到了一个关于 无法创建常量值 MinQty和MaxQty是int,所以我不知道是否需要添加以转换为Convert.ToInt32 无法创建“匿名类型”类型的常量值。在此上下文中仅支持基元类型或枚举类型 这将生成一个列表对象 var listOfLicense = (from l in db.License select new { l.ProductId, l.MinLicense, l.Ma

我正试图在实体框架中使用匿名类型,但我得到了一个关于

无法创建常量值

MinQty和MaxQty是int,所以我不知道是否需要添加以转换为Convert.ToInt32

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

这将生成一个列表对象

var listOfLicense = (from l in db.License
    select new
    {
        l.ProductId,
        l.MinLicense,
        l.MaxLicense
    }).tolist();
这是一个较大的EF对象,我在那里得到错误,我是否丢失了一个铸造

var ShoppingCart = (from sc in db.ShoppingCarts
Select new model.Shoppingchart{
   ShoppingCartId= sc.Id,
    MinQty = (int)listOfLicense 
        .Where(mt => (int)mt.ProductId == sc.ProductId)
        .Select(mt => (int)mt.MinLicense)
        .Min(mt => mt.Value),
    MaxQty = (int)listOfLicense 
        .Where(mt => (int)mt.ProductId == p.ProductId)
        .Select(mt =>(int) mt.MaxQty)
        .Max(mt => mt.Value)}.tolist();
这将生成一个列表对象

var listOfLicense = (from l in db.License
    select new
    {
        l.ProductId,
        l.MinLicense,
        l.MaxLicense
    }).tolist();
var listOfLicense=从db.License中的l开始 选择新的 { l、 ProductId, l、 MinLicense, l、 MaxLicense }

上面的示例不构建对象列表。它构建一个查询以返回该匿名类型的对象

这将在内存中生成该类型对象的列表:

var listOfLicense = (from l in db.License
  select new
  {
      l.ProductId,
      l.MinLicense,
      l.MaxLicense
  }).ToList();
这里使用.ToList将执行查询并返回匿名类型的具体化列表。从这里开始,您的代码可以毫无例外地按预期工作。但是,这实际上是从数据库表中的所有行加载3列,随着系统的成熟和行的添加,这可能是一个问题

你得到的错误不是演员的问题,而是翻译的问题。因为您的初始查询仍然只是一个EF查询,所以任何针对它的进一步查询都需要符合EF限制。EF必须能够将表达式试图选择的内容转换回SQL。在您的情况下,真正的代码试图做的是打破这些规则

通常,最好让EF处理IQueryable,而不是将整个列表具体化到内存中。尽管要做到这一点,我们需要看到真实的代码,或者是一个最小的可复制示例

此代码:

MinQty = (int)listOfLicense 
    .Where(mt => (int)mt.ParentProductId == p.ProductId)
    .Select(mt => (int)mt.MinLicense)
    .Min(mt => mt.Value),
。。。不适合上述匿名类型,因为mt.ParentProductId与匿名类型之间没有关联。p似乎与该类型相关,而不是mt,因此您的示例中似乎缺少很多查询代码

编辑:根据更新的示例:

var ShoppingCart = (from sc in db.ShoppingCarts
Select new model.Shoppingchart{
   ShoppingCartId= sc.Id,
    MinQty = (int)listOfLicense 
        .Where(mt => (int)mt.ProductId == sc.ProductId)
        .Select(mt => (int)mt.MinLicense)
        .Min(mt => mt.Value),
    MaxQty = (int)listOfLicense 
        .Where(mt => (int)mt.ProductId == p.ProductId)
        .Select(mt =>(int) mt.MaxQty)
        .Max(mt => mt.Value)}.ToList();
根据ShoppingCart、产品和许可证之间的关系,可以将类似的内容构建到单个查询表达式中。看起来许可证实际上指的是一种产品,它包含您感兴趣的最小和最大数量

假设结构如下:

public class Product 
{
    [Key]
    public int ProductId { get; set; }
    public int MinQuantity { get; set; }
    public int MaxQuantity { get; set; } 
    // ...
}

// Here lies a question on how your shopping cart to product relationship is mapped.  I've laid out a many-to-many relationship using ShoppingCartItems

public class ShoppingCart
{
    [Key]
    public int ShoppingCartId { get; set; }
    // ...

    public virtual ICollection<ShoppingCartItem> ShoppingCartItems { get; set; } = new List<ShoppingCartItem>();
}

public class ShoppingCartItem
{
    [Key, Column(0), ForeignKey("ShoppingCart")]
    public int ShoppingCartId { get; set; }
    public virtual ShoppingCart ShoppingCart{ get; set; }
    [Key, Column(1), ForeignKey("Product")]
    public int ProductId { get; set; }
    public virtual Product Product { get; set; }
}
这将提供一个购物车列表,每个购物车包含一个产品列表及其各自的最小/最大数量

如果您还希望购物车中所有产品的最低最低数量和最高数量:

var shoppingCarts = db.ShoppingCarts
    .Select(sc => new model.ShoppingCart
    {
       ShoppingCartId = sc.Id,
       Products = sc.ShoppingCartItems
          .Select(sci => new model.Product
          {
              ProductId = sci.ProductId,
              MinQuantity = sci.MinQuantity,
              MaxQuantity = sci.MaxQuantity
          }).ToList(),
       OverallMinQuantity = sc.ShoppingCartItems
           .Min(sci => sci.MinQuantity),
       OverallMaxQuantity = sc.ShoppingCartItems
           .Max(sci => sci.MaxQuantity),

    }).ToList();
虽然我不确定这样的数字与购物车结构的关系有多实际。在任何情况下,通过为实体之间的关系设置导航属性,EF应该完全能够为要检索的数据构建IQueryable查询,而无需诉诸预取列表。预取并将这些列表重新引入进一步查询的一个问题是EF最多可以处理行。与SQL IN子句一样,最多可以从一个集合中解析项


在任何情况下,它听起来好像为您提供了一些想法,让您尝试获得所需的数字。

Min和Max是聚合函数,您在同一个查询中调用了两个不同的聚合,可能与您没有显示的其他内容混合在一起。您可能需要单独的数据库查询或切换到客户端evaluation@camilo-特列文托:这是唯一的办法吗?我不能用子部分来做这件事?这取决于你的整个查询是做什么的,但你没有发布所有内容。如果你的查询可以与GroupBy一起使用,那么你可以继续使用一个查询,否则你需要多个查询/客户端评估I用一些额外的代码更新帖子我可以使用IQueryable吗?我根据你更新的代码对答案进行了一点扩展,但听起来你已经在你想要完成的事情上取得了进展。