Linq 使用EF中的中间实体进行多对多查询

Linq 使用EF中的中间实体进行多对多查询,linq,entity-framework,angularjs,breeze,Linq,Entity Framework,Angularjs,Breeze,我正在使用EF6、WebApi2、AngularJS和BreezeJs 我拥有以下实体: Person { public string Name { get; set;} public virtual ICollection<GenericProfileCountry> Countries { get; protected set; } } public class GenericProfileCountry { public string PersonId{

我正在使用EF6、WebApi2、AngularJS和BreezeJs

我拥有以下实体:

Person
{
   public string Name { get; set;}
   public virtual ICollection<GenericProfileCountry> Countries { get; protected set; }
}

public class GenericProfileCountry
{
    public string PersonId{ get; set; }
    public virtual Person Person{ get; set; }
    public string CountryIso { get; set; }
    public virtual Country Country { get; set; }
}

public class Country
{
    public string Iso { get; set; }
    public string Name { get; set; }
}
我想做的是使用中间实体上的条件对上述查询执行where语句。所以说我只想带他们的第一个国家(一个人可以有多个国家)iso代码等于“GB”的联系人

在Linq中,它类似于Contacts.Where(contact=>contact.Countries.First().CountryIso=='GB')


在breeze的where(谓词)中可以表达类似的东西吗?我想换一种方法(从中间表开始,然后从中间表开始过滤),但不确定这是否是正确的方法。

您可以通过使用关键字
any
all
创建谓词来实现这一点

.where('Countries','any','CountryIso','eq','GB')
如果要在孙子辈上创建谓词:

编辑

如果您想获得国家/地区以“GB”开头的第一个联系人,您可以通过以下方式实现:

  • 杰伊的建议

  • 在Breeze控制器上使用Linq:

    public IQueryable<Person> ContactsWithFilteredCountryIso(string CountryIso)
        {
            return _contextProvider.Context.Persons.Where(p => p.Countries.First().CountryIso== CountryIso);
         }
    

    通过发布BREESE国家查询并扩大联系,可以对有联系的国家编写选择性预测:

    return zEntityQuery.from('Countries').expand('Contact')
            .select('Country.name')
            .where('CountryIso','eq','GB')
            .orderBy(contactOrderBy)
            .toType(entityName)
            .using(self.manager).execute()
            .to$q(querySucceeded, self._queryFailed);
    

    拿countries集合中的第一个项目而不是任何项目怎么样?我想不出任何方法,除非你在一个国家实例本身上有一些属性表明它是第一个。然后您可以在子查询中添加一个表达式,如“and”(“SeqNum”,“eq”,1)”@JayTraband,这正是我想说的。另一种方法是,如果他想在每种方法中都获得第一,他可以将Linq作为webMethod来使用collection@AdelSal你能详细说明一下吗?另外,虽然不是在最初的问题中,但使用breeze的.select方法选择该国的方法是什么?@mxa055详细说明Jay的建议?还是Web方法?在同一字段上使用条件时,为什么要选择CountryIso?所有的值都将是相同的,在本例中为“GB”。如果有人知道如何对breeze的Select命令执行相同的操作(即选择投影而不是完整实体),那么最好知道
    return zEntityQuery.from('Contacts')
            .withParameters({ CountryIso: "GB"})
            .expand('Profile, Countries')
            .orderBy(contactOrderBy)
            .toType(entityName)
            .using(self.manager).execute()
            .to$q(querySucceeded, self._queryFailed);
    
    return zEntityQuery.from('Countries').expand('Contact')
            .select('Country.name')
            .where('CountryIso','eq','GB')
            .orderBy(contactOrderBy)
            .toType(entityName)
            .using(self.manager).execute()
            .to$q(querySucceeded, self._queryFailed);