tsunami

log in
history

C# 3.0 Syntax reduction

Luke Breuer
2008-03-15 18:36 UTC
tags: c#-3.0 linq

C# 2.0, excerpt
    Dictionary<UniqueIdentifier, QueryField> d = new Dictionary<UniqueIdentifier, QueryField>();
    Dictionary<UniqueIdentifier, List<QueryField>> invalidSets = new Dictionary<UniqueIdentifier, List<QueryField>>();

    foreach (QueryField qf in fields)
    {
        UniqueIdentifier key = qf.GetUniqueIdentifier(UniqueIdentifierType.QueryField);
        QueryField firstQf;

        if (d.TryGetValue(key, out firstQf))
        {
            List<QueryField> qfs;

            if (!invalidSets.TryGetValue(key, out qfs))
                invalidSets[key] = new List<QueryField>(new QueryField[] { firstQf, qf });
            else
                qfs.Add(qf);
        }
        else
        {
            d[key] = qf;
        }
    }
C# 3.0, exerpt
    var invalidSets = fields
        .GroupBy(qf => qf.GetUniqueIdentifier(UniqueIdentifierType.QueryField))
        .Where(g => g.Count() > 1)
        .ToDictionary(g => g.Key, g => g.AsEnumerable());
C# 2.0, full version
private void VerifyFieldUniqueness(List<QueryField> fields)
{
    Dictionary<UniqueIdentifier, QueryField> d = new Dictionary<UniqueIdentifier, QueryField>();
    Dictionary<UniqueIdentifier, List<QueryField>> invalidSets = new Dictionary<UniqueIdentifier, List<QueryField>>();

    foreach (QueryField qf in fields)
    {
        UniqueIdentifier key = qf.GetUniqueIdentifier(UniqueIdentifierType.QueryField);
        QueryField firstQf;

        if (d.TryGetValue(key, out firstQf))
        {
            List<QueryField> qfs;

            if (!invalidSets.TryGetValue(key, out qfs))
                invalidSets[key] = new List<QueryField>(new QueryField[] { firstQf, qf });
            else
                qfs.Add(qf);
        }
        else
        {
            d[key] = qf;
        }
    }

    if (invalidSets.Count == 0)
        return;

    // functional programming, ahoy!
    Converter<QueryField, string> qfToString = delegate(QueryField qf)
    {
        return string.Format("pk: {0}, qtc: {1}, criteria1: '{2}'",
            qf.Pk, qf.QueryTemplateCategoryFk, qf.CriteriaValue1 != null ? qf.CriteriaValue1 : "<null>");
    };
    Converter<List<QueryField>, string> listToString = delegate(List<QueryField> list)
    {
        return "[" + string.Join(", ", list.ConvertAll(qfToString).ToArray()) + "]";
    };
    Converter<Dictionary<UniqueIdentifier, List<QueryField>>, string> dictToString =
    delegate(Dictionary<UniqueIdentifier, List<QueryField>> dict)
    {
        return string.Join("\n", new List<List<QueryField>>(dict.Values).ConvertAll(listToString).ToArray());
    };

    throw new ArgumentException(string.Format(
        "Found multiple query fields with the same PK and the same QTC; this is not permissible because other " +
        "code makes the assumption that these two fields together are unique within the set of QFs in a query.  " +
        "The invalid query field set(s) are as follows:\n{0}", dictToString(invalidSets)));
}
C# 3.0, full version
private void VerifyFieldUniqueness(List<QueryField> fields)
{
    var invalidSets = fields
        .GroupBy(qf => qf.GetUniqueIdentifier(UniqueIdentifierType.QueryField))
        .Where(g => g.Count() > 1)
        .ToDictionary(g => g.Key, g => g.AsEnumerable());

    if (!invalidSets.Any())
        return;

    // functional programming, ahoy!
    Func<QueryField, string> qfToString = qf =>
        string.Format("pk: {0}, qtc: {1}, criteria1: '{2}'",
            qf.Pk, qf.QueryTemplateCategoryFk, qf.Criteria.Value1 ?? "<null>");
    Func<IEnumerable<QueryField>, string> listToString = list =>
        "[" + list.Select(qfToString).Join(", ") + "]";
    Func<Dictionary<UniqueIdentifier, IEnumerable<QueryField>>, string> dictToString = dict =>
        dict.Values.Select(listToString).Join("\n");

    throw new ArgumentException(
        "Found multiple query fields with the same PK and the same QTC; this is not permissible because other " +
        "code makes the assumption that these two fields together are unique within the set of QFs in a query.  " +
        "The invalid query field set(s) are as follows:\n" + dictToString(invalidSets));

}