Breeze在EF中查询具有中间实体的多对多

时间:2014-03-31 12:36:15

标签: 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{ 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; }
}

现在我有一个查询,让所有人轻松如下:

return zEntityQuery.from('Contacts').expand('Profile, Countries')
        .orderBy(contactOrderBy)
        .toType(entityName)
        .using(self.manager).execute()
        .to$q(querySucceeded, self._queryFailed);

我想要做的是使用中间实体上的条件对上述查询执行where语句。所以说我想只带来他们的第一个国家(一个人可以拥有多个国家)iso代码等于&#39; GB&#39;。

在Linq中它会像Contacts.Where(contact =&gt; contact.Countries.First()。CountryIso ==&#39; GB&#39;)

在微风的哪里(谓词)可以表达类似的东西吗?我想到了另一种方式(从中间表开始并从那里过滤),但不确定这是否是正确的方法。

2 个答案:

答案 0 :(得分:3)

您可以通过使用关键字anyall

创建谓词来实现这一目标
.where('Countries','any','CountryIso','eq','GB')

如果您想在大孩子上创建一个谓词:BreezeJS Predicates on 2nd level expanded entities

修改

如果您想获得国家Isos以&#39; GB&#39;开头的第一个联系人,您可以通过以下方式实现这一目标:

  1. Jay的建议。

  2. 在Breeze控制器上使用Linq:

    public IQueryable<Person> ContactsWithFilteredCountryIso(string CountryIso)
        {
            return _contextProvider.Context.Persons.Where(p => p.Countries.First().CountryIso== CountryIso);
         }
    
  3. 然后在客户端:

    return zEntityQuery.from('Contacts')
            .withParameters({ CountryIso: "GB"})
            .expand('Profile, Countries')
            .orderBy(contactOrderBy)
            .toType(entityName)
            .using(self.manager).execute()
            .to$q(querySucceeded, self._queryFailed);
    

答案 1 :(得分:0)

在具有启动联系人的国家/地区编写选择投影可以通过在国家/地区发布Breeze查询并扩展联系人来实现:

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);