C# Example of Using LINQ – 1


LINQ stands for Language-Integrated Query, which is made available to .NET programming languages from .NET 2.0. Coding using LINQ gives concise code and essentially it is a query where it does not get executed immediately until whenever needed.

The following code:

1
2
3
4
5
6
7
8
9
10
11
12
public class FileCollection : CollectionBase<string>
{
        public override bool Contains(string item)
        {
            foreach (string str in this)
            {
                if (string.Compare(str, item, true) == 0)
                    return true;
            }
            return false;
        }
}
public class FileCollection : CollectionBase<string>
{
        public override bool Contains(string item)
        {
            foreach (string str in this)
            {
                if (string.Compare(str, item, true) == 0)
                    return true;
            }
            return false;
        }
}

can thus be converted nicely to LINQ:

1
2
3
4
5
6
7
public class FileCollection : CollectionBase<string>
{
        public override bool Contains(string item)
        {
            return this.Any(str => string.Compare(str, item, true) == 0);
        }
}
public class FileCollection : CollectionBase<string>
{
        public override bool Contains(string item)
        {
            return this.Any(str => string.Compare(str, item, true) == 0);
        }
}

There is no performance difference so clearly the LINQ version is the winner!

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
179 words
Last Post: Disallow Multiple Instance of Applications on The Same Machine (Windows Server) by Other User Sessions (Both C# and C++ solution)
Next Post: The Javascript Function to Compute the Stamp Duty Tax

The Permanent URL is: C# Example of Using LINQ – 1

Leave a Reply