A day with .Net

My day to day experince in .net

Archive for the ‘LINQ’ Category

LINQ to XML – Null handling by extension method

Posted by vivekcek on November 5, 2012

I am using extension methods to handle blank or null elements in xml processing using LINQ.
My methods are given below.

        
	private static string isElementNull(this XElement element)
        {
            if (element != null)
                return element.Value;
            else
                return "";
        }

        private static string isAttributeNull(this XElement element, string attributeName)
        {
            if (element == null)
                return "";
            else
            {
                XAttribute attribute = element.Attribute(attributeName);
                return attribute == null ? "" : attribute.Value;
            }
        }

Now how to access element and its attribute in linq.

strng name=q.Element("Name").isElementNull();
string id=q.isAttributeNull("id");

where q is an element.

Posted in LINQ | Tagged: , | Leave a Comment »

Partitioning Data skip,skip-while,take,take-while

Posted by vivekcek on October 8, 2012

 string[] test5 = new string[] { "an", "apple", "a", "day", "keeps", "the", "doctor", "away" };
            IEnumerable<string> take = test5.Take(3);
            IEnumerable<string> skip = test5.Skip(3);
            IEnumerable<string> skipWhile = test5.SkipWhile(x => x.Substring(0, 1) == "a");
            IEnumerable<string> takeWhile = test5.TakeWhile(x => x.Length <= 3);

Posted in LINQ | Leave a Comment »

Sort a list by price ascending order – put null or zero values at the end

Posted by vivekcek on October 8, 2012

In this code we sort list by price in ascending order. But we place the zero price to the end using ThenBy

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Price> prices = new List<Price>()
            {
                new Price(){ name="a", price=5.0},
                new Price(){ name="b", price=4.0},
                new Price(){ name="c", price=2.0},
                new Price(){ name="d", price=0.0},
                new Price(){ name="e", price=0.0}
            };

            List<Price> sorted = prices.OrderBy(x => x.price == 0).ThenBy(x => x.price).ToList();

               
        }
    }
  
    class Price
    {
        public string name;
        public double price;
    }
}

Posted in LINQ | Leave a Comment »

Join two list using lambda expression

Posted by vivekcek on October 8, 2012

 List<Employee> employees = new List<Employee>()
            {
                new Employee(){ id="1", name= "Vivek"},
                 new Employee(){ id="2", name= "Chanthu"}
            };

            List<Address> address = new List<Address>()
            {
                new Address(){ Empid="1", Addres= "Eleyodu"},
                new Address(){ Empid="2", Addres= "Edakkadom"}
            };

            var query = employees.Join(
                address,
                emp => emp.id,
                adrs => adrs.Empid,
                (emp, adrs) => new { EmployeeName = emp, EmployeAddress = adrs })
                .Where(EmployeeAndAddress => EmployeeAndAddress.EmployeeName.id == EmployeeAndAddress.EmployeAddress.Empid).ToList();

            foreach (var item in query)
            {
                Console.WriteLine("Employee Name:{0},Address:{1}", item.EmployeeName.name, item.EmployeAddress.Addres);
            }
class Employee
        {
            public string id;
            public string name;
            public string department;
        }
        class Address
        {
            public string Empid;
            public string Addres;
        }

Posted in LINQ | Tagged: , | Leave a Comment »