Showing posts with label Linq. Show all posts
Showing posts with label Linq. Show all posts

Monday, January 09, 2012

Add Item to collection if item not exist

Tnx to Miroslav

So, this is extension methods that enables you to add item in collection if item does not exist already


 public static class CollectionsExtensions
    {
        /// 
        /// Adds item to collection if not exist already.
        /// 
        /// The type of the source.
        /// 
The collection.
        /// 
The element.
        /// 
The comparer.
        /// true if item is added to collection.
        public static bool AddIfNotExist(this ICollection collection, TSource element, Func comparer)
        {
            if (collection == null)
            {
                return false;
            }

            if (collection.Any(comparer))
            {
                return false;
            }
            else
            {
                collection.Add(element);
                return true;
            }
        }

        /// 
        /// Adds item to collection if not exist already.
        /// 
        /// The type of the source.
        /// 
The collection.
        /// 
The element.
        /// true if item is added to collection
        public static bool AddIfNotExist(this ICollection collection, TSource element)
        {
            if (collection == null)
            {
                return false;
            }

            if (collection.Contains(element))
            {
                return false;
            }
            else
            {
                collection.Add(element);
                return true;
            }
        }
    }

Wednesday, December 23, 2009

Flatten array of arrays using linq

If you have list of object that contains list of other objects and you want to collect all children in one list linq can make it simple. For example:


List<int> listOfInt = new List<int>();
List<List<int>> listOfListOfInts = new List<List<int>>();
foreach (List<int> ints in listOfListOfInts)
{
foreach (int i in ints)
{
listOfInt.Add(i);
}
}


but using linq the following statements become:


List<int> listOfInt = listOfListOfInts.SelectMany(ints => ints).ToList();

Sunday, November 15, 2009

Using linq to select nodes in tree structure

If we have tree structure of object like this:

class C1
{
public List Children {get; set;}
public string Name {get; set;}
}


and have list of C1 object for example "listOfC1" we can use Linq to select object that match specific name path, for example "N1/N2/N3":


var result =
from c1 in listOfC1
where c1.Name == "N1"
from c2 in c1.Children
where c2.Name == "N2"
from c3 in c2.Children
where c3.Name == "N3"
select c3;

Friday, November 13, 2009

Errors in MSDN samples for LINQ

Microsoft has put on his site samples for LINQ. But on that page there are several errors. For example http://msdn.microsoft.com/en-us/vcsharp/aa336758.aspx#SelectManyCompoundfrom1 explain how to use "compound from". In theirs example it's written:

var pairs =
from a in numbersA,
b in numbersB
where a < b
select new {a, b};

but if you try this code you will get compile error:
"A query body must end with a select clause or a group clause"

The code should be:

var pairs =
from a in numbersA
from b in numbersB
where a < b
select new {a, b};

I am wondering how they gets their results.