Showing posts with label Linq. Show all posts
Showing posts with label Linq. 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

Wednesday, April 11, 2012

How to Order by Multiple in Linq

When Ordering custom collection in Linq  using multiple fields you can use OrderBy and ThenBy methods.

Example

private BOMLineDTO[] SortBOMLines(BOMLineDTO[] bomLines)
{
BOMLineDTO[] sortedBOMLines = null;

if (null != bomLines)
{
sortedBOMLines = bomLines.OrderBy(record => record.ItemDescription, new BOMLineComparer<StringField>()).
ThenBy(record => record.BOMCategoryType, new BOMLineComparer<BasicDataField>()).
ThenBy(record => record.GMTColor, new BOMLineComparer<BasicDataField>()).
ThenBy(record => record.GMTSize, new BOMLineComparer<BasicDataField>()).ToArray();
}
return sortedBOMLines;
}
Here BOMLineComparer<T> is My own class which is derived form IComparer<T> interface


Related Posts:http://prabodha-eranga.blogspot.com/2012/05/linq-orderby-custom-comparer.html