Archive for the ‘Linq’ Category

Using Linq Against a Custom ConfigurationElementCollection

By default, ConfigurationElementCollections don’t implement IEnumerable<T>, so if you are using custom configuration sections you can’t query them with linq. I found a casting solution, but I figured I could do better because the custom configuration classes are under my control, so it would be cleaner to implement the required functionality instead of using the clunky cast in the code consuming the collection. I implemented IEnumerable<T> against the ConfigurationElementCollection. It’s pretty straightforward, just calling the base class BaseGet function and casting the result properly using the As keyword. Now I can execute linq queries against my collection.

using System;

using System.Collections.Generic;

using System.Configuration;

 

namespace Config.Demo

{

    public class MapConfig : ConfigurationSection

    {

        [ConfigurationProperty(“fields”)]

        public MapConfigCollection Fields { get { return this[“fields”] as MapConfigCollection; } }

    }

    public class MapConfigElement : ConfigurationElement

    {

        [ConfigurationProperty(“server”)]

        public string Server { get { return this[“server”] as string; } }

 

        [ConfigurationProperty(“name”)]

        public string Name { get { return this[“name”] as string; } }

 

        [ConfigurationProperty(“id”)]

        public int Id { get { return (int)this[“id”]; } }

    }

    public class MapConfigCollection : ConfigurationElementCollection, IEnumerable<MapConfigElement>

    {

        public MapConfigElement this[int index]

        {

            get { return base.BaseGet(index) as MapConfigElement; }

            set

            {

                if (base.BaseGet(index) != null)

                {

                    base.BaseRemoveAt(index);

                    this.BaseAdd(index, value);

                }

            }

        }

 

        protected override ConfigurationElement CreateNewElement()

        {

            return new MapConfigElement();

        }

 

        protected override object GetElementKey(ConfigurationElement element)

        {

            return ((MapConfigElement)element).Id;

        }

 

        #region IEnumerable<MapConfigElement> Members

 

        public new IEnumerator<MapConfigElement> GetEnumerator()

        {

            int count = base.Count;

            for (int i = 0; i < count; i++)

            {

                yield return base.BaseGet(i) as MapConfigElement;

            }

        }

 

        #endregion

    }

}

Posted on:
Posted in .Net 3.5, Linq | Comments Off on Using Linq Against a Custom ConfigurationElementCollection

LINQ: Selecting in Nested Collections

This took me longer than it should have to figure it out, so I am documenting it for later reuse. The case here is described as such:


I have a collection of person objects, which themselves contain a collection of address objects. I want to find all the addresses in a given city:





public class Address


{


     public string Street { get; set; }


     public string City { get; set; }


     public string Region { get; set; }


     public string Postal { get; set; }


     public AddressType Location { get; set; }


}


 


public class Person


{


     public string Name { get; set; }


     public string ID { get; set; }


     public string Title { get; set; }


     public List<Address> Addresses { get; set; }


}


….



IEnumerable<Address> results = from guy in allPersons


                               from addressResult in guy.Addresses


                               where addressResult City == “London”


                               select addressResult;


I understand this is similar to a join in a SQL statement, but in my case the data does not come from a database but a service where I can’t control data selection in that way.

Posted on:
Posted in .Net 3.5, Linq | Comments Off on LINQ: Selecting in Nested Collections

Linq Foray

I’m working on a website with lots of user controls interacting with one another using ASP.NET Ajax using events. So I end up needing to find other controls often, especially within repeaters. Nested controls all over the place. I found this wonderful post about using an extension method to the Controls Collection that flattens the tree and allows for much simpler Linq queries. The extension method adds and All() function which is seriously useful.