Showing posts with label ienumarable. Show all posts
Showing posts with label ienumarable. Show all posts

Friday, June 29, 2012

Linq to data reader

using linq we can easily extract the data reader values without writing more coding.

example for populating anonymous object from datareader

using (SqlDataReader dataReader = command.ExecuteReader())
{
var annonymousObject = from IDataRecord record in dataReader
select new
{
Code = (string)record["Code"],
Description = (string)record["Description"]
};
}
example for populating custom object from datareader
using (SqlDataReader dataReader = command.ExecuteReader())
{
DataRecord dataRecord= (from IDataRecord record in dataReader
select new DataRecord
{
Code = (string) record["Code"],
Description = (string)record["Description"]
}).ToArray();
}

Wednesday, May 16, 2012

linq orderby custom comparer

I need to orderby Customer Collection.There fore i have to write new class extending with IComparer<T> interface.

public class EntityComparer<T> : IComparer<T> where T : class
    {
        #region IComparer<T> Members

        public int Compare(T x, T y)
        {
            string valuex = string.Empty;
            string valuey = string.Empty;

            if (x is StringField && y is StringField)
            {
                valuex = GetStringFieldValue(x as StringField);
                valuey = GetStringFieldValue(y as StringField);
            }

            if (x is BasicDataField && y is BasicDataField)
            {
                valuex = GetBasicDataFieldValue(x as BasicDataField);
                valuey = GetBasicDataFieldValue(y as BasicDataField);
            }

            return Compare(valuex, valuey);
        }

        #endregion

        #region Helpers

        private string GetBasicDataFieldValue(BasicDataField field)
        {
            string value = string.Empty;

            if (null !=  field && null != field.NewValue && !string.IsNullOrEmpty(field.NewValue.Description))
            {
                value = field.NewValue.Description;
            }

            return value;
        }

        private string GetStringFieldValue(StringField field)
        {
            string value = string.Empty;

            if (null != field && !string.IsNullOrEmpty(field.NewValue))
            {
                value = field.NewValue;
            }

            return value;
        }

        private  int Compare(string x, string y)
        {
            return string.Compare(x, y, true, CultureInfo.InvariantCulture);
        }

        #endregion
    }

Then we can do our linq ordering like bellows
bomLines.OrderBy(record => record.BOMCategoryType, new EntityComparer<BasicDataField>());
Here we have to create new instance of the our Custom comparer class.
Related Posts:http://prabodha-eranga.blogspot.com/2012/04/linq-order-by-multiple.html

Tuesday, March 27, 2012

Return empty IEnumerable<T> in C sharp

In order to return empty IEnumerable  in csharp you can use static Empty<T>() method in Enumerable class.

Suppose i have custom class called Foo  then,

Enumerable.Empty<Foo>();