Breeze 查询不同的值

Breeze 查询不同的值,breeze,Breeze,我现在正努力让它工作好几个小时。我尝试了这些例子并阅读了文档,但我就是想不出来 我想用名称字段从数据库中查询不同的值。 我认为这应该行得通,但行不通。找不到该方法 [HttpGet] public IQueryable<MvlOP> MvlOpsPerson(long mvlId) { System.Data.Entity.Infrastructure.DbQuery<MvlOP> query = ContextProvider.Conte

我现在正努力让它工作好几个小时。我尝试了这些例子并阅读了文档,但我就是想不出来

我想用名称字段从数据库中查询不同的值。 我认为这应该行得通,但行不通。找不到该方法

[HttpGet]
    public IQueryable<MvlOP> MvlOpsPerson(long mvlId)
    {
        System.Data.Entity.Infrastructure.DbQuery<MvlOP> query = ContextProvider.Context.MvlOps;

        query = query.Include("StatusOP");
        return query.Where(op => op.MvlId == mvlId).Distinct(new PropertyComparer<MvlOP>("Id_P"));
    }
ExceptionType=System.NotSupportedException


所以这是错误的。据我所知,breeze不提供查询不同的值。查询全部和筛选不是一个选项。非常感谢您提供的帮助。

我认为问题在于实体框架EF无法使用您的PropertyComparer。EF仅支持不带比较器的Distinct

我发布这篇文章是为了让可能需要它的人可以使用它。也许这可以在某种程度上加以改进。受Breeze DocCode启发,感谢Northwind控制器中的部分类Api方法:

可以通过以下方式查询不同的值: 为了能够使用DifferenticEqualityComparer方法,查询必须作为IEnumerable存储在内存中。EqualityComparer无法转换为SQL语句。 因此,where子句适用,然后比较器过滤结果记录

return query.AsQueryable:要能够使用skip/take和inlineCount,查询必须是IQueryable。因此,该方法是可行的


这很管用,萨沙。让我们提醒读者,AsEnumerable之后的所有事情都发生在服务器内存中。您应该知道,此时的数据集将被检索到服务器,并且该数据集的后续操作(包括来自客户端的独特和任何附加筛选条件)将在服务器上进行,而不是在数据层上。那很好。。。除非数据集可能达到数百万。如果有人想出一个主意让DB这样做,我很乐意用我的解决方案来交换它。到目前为止,我还没有找到。
ExceptionMessage=LINQ to Entities does not recognize the method 'System.Linq.IQueryable`1[MAHN.Model.MvlOP] Distinct[MvlOP](System.Linq.IQueryable`1[MAHN.Model.MvlOP], System.Collections.Generic.IEqualityComparer`1[MAHN.Model.MvlOP])' method, and this method cannot be translated into a store expression.
//The API Method ----------
[HttpGet]
    public IQueryable<MvlOPPersonPartial> MvlOpsPerson(long mvlId)
    {
        var query = (from op in ContextProvider.Context.MvlOps
                      where op.MvlId == mvlId
                      select new MvlOPPersonPartial()
                          {
                              MvlId = op.MvlId,
                              Kundenname = op.Kundenname,
                              Kundennummer = op.Kundennummer,
                              Id_P = op.Id_P
                          })
                          .AsEnumerable()
                          .Distinct(new PropertyComparer<MvlOPPersonPartial>("Id_P"));
        return query.AsQueryable();
    }

public class MvlOp
{
...
    public string Kostenstelle { get; set; }
    public string Betreuer_Id { get; set; }
    public decimal? Id_P { get; set; }
    public string Kundenname { get; set; }
    public string Kundennummer { get; set; }
    public string Person_Typ1 { get; set; }
...
}


//The partial class needed for distinct values ------------
//MvlOP cannot be constructed in an LINQ to Entities query
public class MvlOPPersonPartial
{
    public long MvlId { get; set; }
    public string Kundenname { get; set; }
    public string Kundennummer { get; set; }
    public decimal? Id_P { get; set; }
}


//A generic comparer ---------------
public class PropertyComparer<T> : IEqualityComparer<T>
{
    private PropertyInfo _PropertyInfo;

    /// <summary>
    /// Creates a new instance of PropertyComparer.
    /// </summary>
    /// <param name="propertyName">The name of the property on type T 
    /// to perform the comparison on.</param>
    public PropertyComparer(string propertyName)
    {
        //store a reference to the property info object for use during the comparison
        _PropertyInfo = typeof(T).GetProperty(propertyName,
    BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public);
        if (_PropertyInfo == null)
        {
            throw new ArgumentException(string.Format("{0} is not a property of type {1}.", propertyName, typeof(T)));
        }
    }

    #region IEqualityComparer<T> Members

    public bool Equals(T x, T y)
    {
        //get the current value of the comparison property of x and of y
        object xValue = _PropertyInfo.GetValue(x, null);
        object yValue = _PropertyInfo.GetValue(y, null);

        //if the xValue is null then we consider them equal if and only if yValue is null
        if (xValue == null)
            return yValue == null;

        //use the default comparer for whatever type the comparison property is.
        return xValue.Equals(yValue);
    }

    public int GetHashCode(T obj)
    {
        //get the value of the comparison property out of obj
        object propertyValue = _PropertyInfo.GetValue(obj, null);

        if (propertyValue == null)
            return 0;

        else
            return propertyValue.GetHashCode();
    }

    #endregion
}