0

我现在正试图让它工作几个小时。我尝试了示例并阅读了文档,但我无法弄清楚。

我想使用名称字段从数据库中查询不同的值。我认为这应该有效,但事实并非如此。未找到 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

所以这是错误的。据我了解,微风不提供查询不同的值。查询全部和过滤不是一种选择。任何有关如何做到这一点的帮助都非常感谢。

4

2 回答 2

1

我发布了这个,所以可能需要它的人可以使用它。也许这可以通过某种方式得到改善。(受 Breeze DocCode 启发(感谢 Northwind 控制器中的 Partial Class Api 方法 :))

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

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
}
于 2013-01-10T19:21:18.410 回答
0

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

于 2013-01-10T01:12:34.023 回答