Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# NHibernate Linq:Contains语句_C#_Linq_Nhibernate - Fatal编程技术网

C# NHibernate Linq:Contains语句

C# NHibernate Linq:Contains语句,c#,linq,nhibernate,C#,Linq,Nhibernate,例如,假设您有这样一个类 **Person** int PersonID string PersonName BusinessLocation Locations **BusinessLocation** string city string state List<int> ZipCodes 表示位置可能存在于多个ZipCode中 同样忽略zipcodes应该是字符串而不是int,这只是一个例子 假设业务的位置存在于多个ZipCode中 现在,我正试图撤回person表中的所有人

例如,假设您有这样一个类

**Person**
int PersonID
string PersonName
BusinessLocation Locations

**BusinessLocation**
string city
string state
List<int> ZipCodes
表示位置可能存在于多个ZipCode中 同样忽略zipcodes应该是字符串而不是int,这只是一个例子

假设业务的位置存在于多个ZipCode中

现在,我正试图撤回person表中的所有人,给定一个业务zipcode

例如,我想要所有zipcode为32567的人

给定一个ID列表,这是可行的,我正试图做相反的事情,给定一个ID,我想要一个人员列表

public Person GetPersonsByBusinessZipCode(int zipcode)
{
    List<Person> personList = 
        this.GetAllQueryable().Where(
            x => x.Locations.ZipCodes.Contains(zipcode)).ToList();
}
这是像Fluent中这样映射的

HasMany<int>(x => x.ZipCodes)
   .Table("BusinessLocationsZipCodes")
   .KeyColumns.Add("BusinessLocationID")
   .Inverse()
   .Element("ZipCode")
   .AsBag()
   .Cascade.None()
   .Cache.ReadOnly();
BusinessLocationZipCodes只是一个参考表,暗示一个BusinessLocation可以有多个ZipCodes,因此具有多个ZipCodes

如果给我一个ZipCodes列表,并且我正在尝试查找包含在ZipCodes列表中的BusinessLocations,只要映射到的是一个zipcode而不是一个ZipCodes列表,那么就知道反向操作是有效的。现在我只想找到给定zipcode的商业地点


如果有人有答案,我将不胜感激。

只要您使用System.Linq.Queryable和IQueryable接口,Linq提供程序就不太重要

我相信您正在寻找可查询的。任何方法

Persons.Where(p => p.Locations.ZipCodes.Any(z => z == zipCode))
或者,如果您的人员有多个地点:

IQueryable<Person> query =
  from p in Persons
  where
  (
    from l in p.Locations
    from z in l.ZipCodes
    select z
  ).Any(z => z == zipCode)
  select p;

有什么例外?使用最新的NHibernate build 2.1.2.400,应该可以正常工作

//下面的问题:不用管西班牙语了

var transporte = // some entity;
var solicitud  = IQueryable<Solicitud>
                   .Where(x => x.SolicitudesDeTransporte.Contains(transporte)).ToList();

帕科,显然我搞砸了,因为我有两个账户在这里浮动,我不知道,所以这是我的OpenID账户。无论如何,我不确定NullReferenceException是否存在,它深入到NHibernate名称空间中,没有指向我正在使用的任何类的链接。但是,只有当我尝试使用Contains深入了解它时,才会得到NullReferenceException。如上所述,如果我只是返回ZipCodes列表,我不会得到NullReferenceException。因此,我的映射工作正常,但它似乎无法返回基于Contains语句的BusinessLocation列表。

什么不工作?您是否遇到异常,是否得到错误的结果等。您使用哪种linq提供程序?我使用的是linq to Nhibernate 1.0、Nhibernate 2.1和FluentNhibernate 1.0,不起作用的是,在给定zipcode的情况下,能够撤回业务地点列表,基本上,通过使用Linq对zipcode列表进行预处理。zipcodes是否插入了inverse和cascade none?我得到的异常是对象空引用。但是我可以得到ZipCodes列表,只是它不允许我使用contains进一步查询列表。这就是它抛出异常的时候。您的查询和发布的查询之间的一个区别是,您在contains中使用了一个id为的实体,而在问题中使用的是一个没有id的值对象。@Matt Braunwart:什么是null,不应该为null?谢谢David,但是我收到了一个错误,说明无法理解y=325671。linq提供者很重要,因为它们都有不同的不完整性。NHiberante.Linq.dll中的一个现在不推荐使用。2.对于完全实现的linq提供程序,在技术上使用Anyx=>x==y或Containsy应该无关紧要。Contains是首选,因为它更具可读性。@Matt Braunwart-我的代码中没有使用y,所以我很难解决您的问题。是不是y有一个比zipcode更合适的属性?而且-也许==比较比=assignment@Paco我一定不同意。由于System.Linq.Queryable上缺少Containers此IQueryable源的T项方法,因此我相信Linq作者打算使用canon作为任何此IQueryable源的表达式过滤器。如果您想支持您的立场,请指出在LinqToAnything中首选包含的框架中的一些证据。主干中的Linq实现中不缺少包含。包含适用于大多数查询的内容。您可以在msdn上找到您提到的contains扩展方法。在where子句的表达式中由linq提供程序实现的方法不一定需要是IQueryable的扩展。Zipcodes属性可能无法读取。如果您认为Anyx=>x==y比Containsy更具可读性,当然最好使用Any。我不知道如何证明我实际上更喜欢一种方式而不是另一种方式。我试图解释你的答案是关于可读性的。很抱歉,没有正确说明,唯一的链接是指向我正在使用的类,即调用调用的存储库。
SELECT this_.Id as Id6_0_,
       /* etc... */
FROM   Solicitud this_
WHERE  this_.Id in (SELECT this_0_.Id as y0_
                    FROM   Solicitud this_0_
                           left outer join Solicitud_Transporte solicitude1_
                             on this_0_.Id = solicitude1_.Id_Solicitud
                    WHERE  solicitude1_.Id = 1 /* :p0 */)