LINQ不同的条目

时间:2013-07-23 15:33:21

标签: linq

我有一个包含约会的数据库。可以同时安排多个约会。我想检索一个不包含重复次数的可用预约时间列表。

修改

例如,我可以在我的数据库中

REQUEST_ID    REQUESTER_ID   APPOINTMENT_TIME
     1             0        30-JUN-13 03.30.00 AM
     2             1        30-JUN-13 03.30.00 AM
     3             0        30-JUN-13 03.30.00 AM
     4             0        30-JUN-13 03.30.00 AM
     5             3        30-JUN-13 03.35.00 AM
     6             0        30-JUN-13 03.45.00 AM
     7             0        30-JUN-13 03.45.00 AM

我想要返回

REQUEST_ID    REQUESTER_ID   APPOINTMENT_TIME
     1             0        30-JUN-13 03.30.00 AM
     6             0        30-JUN-13 03.45.00 AM

结束编辑

此查询返回所有可用的预约空档

var data =
    (from a in db.FLU_SHOT
      where a.REQUESTER_ID == 0
      select a).ToArray();

此查询仅返回时间,我将丢失数据库中的其余信息

var times =
   (from a in data
    orderby Convert.ToDateTime(a.APPOINTMENT_TIME)
    select a.APPOINTMENT_TIME).Distinct().ToArray();

此查询返回与我的第一个查询相同的信息

var times =
   (from a in data
    orderby Convert.ToDateTime(a.APPOINTMENT_TIME)
    select a).Distinct().ToArray();

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:2)

您正在寻找MoreLinqDistinctBy

var times =
   (from a in data
    orderby Convert.ToDateTime(a.APPOINTMENT_TIME)
    select a).DistinctBy(a => a.APPOINTMENT_TIME).ToArray();

或者,您可以使用基本的GroupBy方法:

var times =
   (from a in data
    orderby Convert.ToDateTime(a.APPOINTMENT_TIME)
    select a).GroupBy(a => a.APPOINTMENT_TIME).Select(g => g.First()).ToArray();

或我的自定义lambda相等比较器(这比GroupBy快,但比Servy多次指出的DistinctBy慢:

var times =
   (from a in data
    orderby Convert.ToDateTime(a.APPOINTMENT_TIME)
    select a).Distinct(PropertyEqualityComparer.GetNew(a => a.APPOINTMENT_TIME))
             .ToArray();

以下是代码:

public class PropertyEqualityComparer<TObject, TProperty> 
    : IEqualityComparer<TObject>
{
    Func<TObject, TProperty> _selector;
    IEqualityComparer<TProperty> _internalComparer;
    public PropertyEqualityComparer(Func<TObject, TProperty> propertySelector,
        IEqualityComparer<TProperty> innerEqualityComparer = null)
    {
        _selector = propertySelector;
        _internalComparer = innerEqualityComparer;
    }
    public int GetHashCode(TObject obj)
    {
        return _selector(obj).GetHashCode();
    }
    public bool Equals(TObject x, TObject y)
    {
        IEqualityComparer<TProperty> comparer = 
            _internalComparer ?? EqualityComparer<TProperty>.Default;
        return comparer.Equals(_selector(x), _selector(y));
    }
}
//and here's a class to help instantiate it with anonymous objects
public static class PropertyEqualityComparer
{
    public static PropertyEqualityComparer<TObject, TProperty>
        GetNew<TObject, TProperty>(Func<TObject, TProperty> propertySelector)
    { 
        return new PropertyEqualityComparer<TObject, TProperty>
            (propertySelector);
    }
    public static PropertyEqualityComparer<TObject, TProperty>
        GetNew<TObject, TProperty>
        (Func<TObject, TProperty> propertySelector, 
        IEqualityComparer<TProperty> comparer)
    { 
        return new PropertyEqualityComparer<TObject, TProperty>
            (propertySelector, comparer);
    }
}

答案 1 :(得分:2)

您可以使用GroupBy

var times = data.GroupBy(d => Convert.ToDateTime(d.APPOINTMENT_TIME))
    .Select(g => g.First())
    .ToArray();

答案 2 :(得分:1)

每当我尝试使用LINQ(最终查询数据库)并且答案不明显时,我首先编写SQL语句,然后找出如何将其转换为LINQ。

如果您无法弄清楚如何进行翻译,那么您可以在线找到一个工具来帮助,或者您可以将查询转换为存储过程或UDF并从数据上下文中调用它。

在中等到高度复杂的查询中,手动编写它几乎总是更好。最有可能的是,无论LINQ吐出的内容都不会针对您的特定情况进行优化。你通常可以哄骗LINQ输出你想要的查询,但到那时你已经浪费了很多宝贵的时间,可能会花费更多的时间来做其他事情。

编辑: 为了更直接地回答您的问题,您似乎主要对APPOINTMENT_TIME感兴趣。在这种情况下,在该列上执行一个组,然后您可以遍历每个组并获取时间。