查询不同的值

时间:2013-01-10 00:51:44

标签: breeze

我想让这个工作几个小时了。我尝试了这些示例并阅读了文档,但我无法弄明白。

我想使用名称字段从DB查询不同的值。 我认为这应该有效,但事实并非如此。未找到Distinct方法

[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"));
    }

我收到以下错误:

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.

ExceptionType = System.NotSupportedException

所以这是错的。据我所知,微风不提供查询不同的价值观。查询所有内容并过滤不是一种选择。关于如何做到这一点的任何帮助非常感谢。

2 个答案:

答案 0 :(得分:1)

我发布此消息,以便可能需要它的人可以使用它。也许这可以通过某种方式得到改善。 (受Breeze DocCode的启发(感谢Northwind Controller中的Partial Class Api方法:))

可以通过以下方式查询不同的值: 为了能够使用Distinct(IEqualityComparer)方法,查询必须在Memory中作为IEnumerable。 EqualityComparer无法转换为SQL语句。 因此,where子句适用,然后由Comparer过滤生成的记录。

return query.AsQueryable():为了能够使用skip / take和inlineCount,查询必须是IQueryable<T>。因此,方法AsQueryable()

//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
}

答案 1 :(得分:0)

我认为问题在于实体框架(EF)无法使用您的PropertyComparer。 EF仅在没有比较器的情况下支持Distinct。