无法创建类型为' System.Int32 []'的null常量值。

时间:2016-03-25 09:21:26

标签: c# entity-framework linq

使用以下

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="large-amount-file.css">
    </head>
    <body>
        <p>If you click on me, I will disappear.</p>
        <p>Click me away!</p>
        <p>Click me too!</p>
        <p>fsdfsd</p>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                alert("hi");
            });
        </script>
    </body>
</html>

我收到此错误

  

无法创建类型为&#39; System.Int32 []&#39;的null常量值。只要   支持实体类型,枚举类型或基元类型   这个背景。

我明白了错误,但我无法弄清楚应该如何修复它。 PagedData.Products = from p in db.Products where (from m in p.Manufacturers where model.man.Contains(m.ManufacturerID) select m).Any() where (from s in p.Sizes where model.size.Contains(s.SizeID) select s).Any() where (from c in p.Colors where model.color.Contains(c.ColorID) select c).Any() select p; model.manmodel.size是整数数组,也可以为null。

2 个答案:

答案 0 :(得分:3)

由于所有条件必须为true才能传递任何Product,因此您应首先检查所有数组是否都包含任何内容:

if (model.man != null && model.size != null && model.color != null
      && model.man.Any() && model.size.Any() && model.color.Any())
{
    PagedData.Products = from p in db.Products ...

如果您事先知道它不会返回任何数据,那么您现在不会执行查询。并且它不会抛出异常,因为您从未使用null数组运行查询。

答案 1 :(得分:3)

我更希望使用方法语法和Where动态构建if子句,但如果您希望在查询中嵌入条件,则需要确保IEnumerable s您用于Contains条件的标准不是null。这应该发生在外面查询:

var man = model.man ?? Enumerable.Empty<int>();
var size = model.size ?? Enumerable.Empty<int>();
var color = model.color ?? Enumerable.Empty<int>();

PagedData.Products = from p in db.Products
                                     where (from m in p.Manufacturers
                                            where man.Any() && man.Contains(m.ManufacturerID)
                                            select m).Any()
                                     where (from s in p.Sizes
                                            where size.Any() && size.Contains(s.SizeID)
                                            select s).Any()
                                     where (from c in p.Colors
                                            where color.Any() && color.Contains(c.ColorID)
                                            select c).Any()
                                     select p;

请注意filter.Any() && filter.Contains(...)毫无意义,相当于filter.Contans(...)。如果要忽略空过滤器,则应使用!filter.Any() || filter.Contans(...)

所以IMO你的查询应该是这样的

var man = model.man ?? Enumerable.Empty<int>();
var size = model.size ?? Enumerable.Empty<int>();
var color = model.color ?? Enumerable.Empty<int>();

PagedData.Products = from p in db.Products
                     where (from m in p.Manufacturers
                            where !man.Any() || man.Contains(m.ManufacturerID)
                            select m).Any()
                     where (from s in p.Sizes
                            where !size.Any() || size.Contains(s.SizeID)
                            select s).Any()
                     where (from c in p.Colors
                            where !color.Any() || color.Contains(c.ColorID)
                            select c).Any()
                     select p;

或者

var query = db.Products.AsQueryable();
if (model.man != null && model.man.Length > 0)
    query = query.Where(p => p.Manufacturers.Any(m => model.man.Contains(m.ManufacturerID)));
if (model.size != null && model.size.Length > 0)
    query = query.Where(p => p.Sizes.Any(s => model.size.Contains(s.SizeID)));
if (model.color != null && model.color.Length > 0)
    query = query.Where(p => p.Colors.Any(c => model.color.Contains(c.ColorID)));
PagedData.Products = query;