LINQ to Entities无法识别方法'Boolean Contains [Decimal]

Tri*_*ack 3 linq asp.net-mvc

我是LINQ的新手,所以我在这里很困惑.我有一个数据库,并尝试运行以下代码.

IQueryable<decimal> location_ids = (from m in _db.Admins
                                    where m.UserId.Equals("c5d3dc0e-81e6-4d6b-a9c3-faa802e10b7d")
                                    select m.LocationId);
if (!location_ids.Contains(new Decimal(conf.umisteni.Budova.ID)))
Run Code Online (Sandbox Code Playgroud)

在if语句中,我得到一个错误,我不明白,也不知道,如何解决它:

System.NotSupportedException: LINQ to Entities does not recognize the method 'Boolean Contains[Decimal](System.Linq.IQueryable`1[System.Decimal], System.Decimal)' method, and this method cannot be translated into a store expression.
  at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.DefaultTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Yan*_*ton 8

使用Linq-to-Objects IEnumerable将允许您对查询结果使用Contains(Decimal).

IEnumerable<decimal> location_ids = (from m in _db.Admins
                                    where m.UserId.Equals("c5d3dc0e-81e6-4d6b-a9c3-faa802e10b7d")
                                    select m.LocationId);
Run Code Online (Sandbox Code Playgroud)

但是,为什么不扩展where子句:

IEnumerable<decimal> location_ids = (from m in _db.Admins
                                    where m.UserId.Equals("c5d3dc0e-81e6-4d6b-a9c3-faa802e10b7d") && (m.LocationId == conf.umisteni.Budova.ID)
                                    select m.LocationId);
if (!location_ids.Any())
Run Code Online (Sandbox Code Playgroud)

  • 你可以用Any()替换.Count()== 0 (6认同)