-3

基本上这是我在数据层中的代码。我只是遇到了倒数第二行代码的问题。我正在尝试将变量“distinctIdsWoring”插入到列表 promoCodeValues 中,但出现错误。

    public static List<PromotionalCodeValue> GetPromotionalCodeValues(string Platform)
    {
        SqlConnection conn = xxxConnection();
        SqlCommand comm = new SqlCommand("GetPromotionalCodeValues", conn);
        comm.CommandType = CommandType.StoredProcedure;

        comm.Parameters.AddWithValue("@Platform", Platform);

        conn.Open();

        List<PromotionalCodeValue> promoCodeValues = new List<PromotionalCodeValue>();

        try
        {
            SqlDataReader dataReader = comm.ExecuteReader();

            while (dataReader.Read())
            {
                promoCodeValues.Add(new PromotionalCodeValue(dataReader));
            }
        }
        finally
        {
            conn.Close();
        }

        promoCodeValues.Clear();

        var distinctIdsWorking = promoCodeValues.AsEnumerable()
                .Select(s => new
                {
                    id = s.Value,
                })
                .Distinct().ToList();

        promoCodeValues = distinctIdsWorking; //????????????????????????????

        return promoCodeValues;

    }

谢谢

4

5 回答 5

3

您正在分配distinctIdsWorkingpromoCodeValues类型不兼容。

而是尝试IEqualityComparer使用Distinct.

public class PromotionalCodeEqualityComparer
    : IEqualityComparer<PromotionalCodeValue>
{
    public bool Equals(PromotionalCodeValue x, PromotionalCodeValue y)
    {
        return x.Value == y.Value;
    }

    public int GetHashCode(PromotionalCodeValue obj)
    {
        return obj.Value != null ? obj.Value.GetHashCode() : 0;
    }
}

用法:

var distinctIdsWorking = promoCodeValues.Distinct(new PromotionalCodeEqualityComparer());

return distinctIdsWorking;

您还清除了promoCodeValueswhich 将不返回任何内容并且您缺少using语句。

更正:

var promoCodeValues = new List<PromotionalCodeValue>();

using(var connection = xxxConnection())
using(var command = new SqlCommand("GetPromotionalCodeValues", connection))
{
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.AddWithValue("@Platform", Platform);

    connection.Open();

    using(var reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            promoCodeValues.Add(new PromotionalCodeValue(reader));
        }
    }
}

return promoCodeValues.Distinct(new PromotionalCodeEqualityComparer()).ToList();
于 2013-11-14T15:58:42.027 回答
2

distinctIdsWorking不是 List<PromotionalCodeValue>,因此您不能将其分配给promoCodeValues

看起来您正试图确保您的结果中每个 ID 只获得一个促销代码。尝试这样的事情:

public static List<PromotionalCodeValue> GetPromotionalCodeValues(string Platform)
{
    // This ensures that your connection gets closed even if there's
    // an exception thrown. No need for a try/finally
    using (SqlConnection conn = xxxConnection())
    {
        SqlCommand comm = new SqlCommand("GetPromotionalCodeValues", conn);
        comm.CommandType = CommandType.StoredProcedure;

        comm.Parameters.AddWithValue("@Platform", Platform);

        conn.Open();

        List<PromotionalCodeValue> promoCodeValues = new List<PromotionalCodeValue>();

        SqlDataReader dataReader = comm.ExecuteReader();

        while (dataReader.Read())
        {
            promoCodeValues.Add(new PromotionalCodeValue(dataReader));
        }

        promoCodeValues = 
            (from pc in promoCodeValues
            // Group by ID, then choose the first item from each group;
            // This is effectively the same as "DistinctBy" but requires
            // no extra methods or classes.
            group pc by pc.Value into g
            select g.First())
                .ToList();

        return promoCodeValues;
    }
}
于 2013-11-14T15:53:05.340 回答
2

您正在清除列表,promoCodeValues.Clear();因此distinctIdsWorking始终是一个空列表,其中没有任何元素。

于 2013-11-14T15:59:59.847 回答
1

为了简化这一点,首先构建一个相等比较器:

public class PromotionalCodeValueEqualityComparer :
    IEqualityComparer<PromotionalCodeValue>
{
    public bool Equals(PromotionalCodeValue x, PromotionalCodeValue y)
    {
        return x.Value == y.Value;
    }

    public int GetHashCode(PromotionalCodeValue x)
    {
        return x.Value.GetHashCode();
    }
}

然后你可以这样做:

return promoCodeValues.Distinct(new PromotionalCodeValueEqualityComparer());
于 2013-11-14T15:56:41.823 回答
1

一个 var distinctIdsWorking = promoCodeValues.AsEnumerable() .Select(s => new { id = s.Value, }) .Distinct().ToList();

仔细看:viaSelect(s => new { id = s.Value })语句生成的匿名对象只有 ID 字段,之后不是上一个类(PromotionalCodeValue)。

您所需要的只是安装MoreLINQ包或从该框架中截取此代码扩展:

public static IEnumerable<TSource> DistinctBy<TSource, TKey> (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
    HashSet<TKey> seenKeys = new HashSet<TKey>();
    foreach (TSource element in source)
    {
        if (seenKeys.Add(keySelector(element)))
        {
            yield return element;
        }
    }
}

并使用这个:DistinctBy(x => x.ID)或者如果您需要多个键:DistinctBy(x => { x.ID, x.Name })

所以你可以重写你的代码: var distinctIdsWorking = promoCodeValues.AsEnumerable().DistinctBy(code => code.Value).ToList();

于 2013-11-14T15:57:46.740 回答