Saturday, July 21, 2012

LINQ


1) Which of the following will facilitate you where method in object of MyCollection
a) MyCollection<int> : IEnumerable<int>
b) MyCollection : IEnumerable<int>
c) MyCollection : IEnumerable
d) MyCollection<int> : IEnumerable

Ans - a and b.
NOTE- "Where" method is available in generic IEnumerable.

2) What will be the output of following program?
var primes = new List<int>() { 1,2,3,8,5,12,78,09,23};
var query = from num in primes
where num < 5
select num;
foreach (var i in query)
{
Console.WriteLine(i);
}
Ans -
1
2
3

3) Write Above example in form of filter(where) and projection(select) format.
Ans -
var primes = new List<int>() { 1,2,3,8,5,12,78,09,23};          
var query = primes.Where(num => num < 5).Select(num1 => num1);
foreach (var i in query)
{
Console.WriteLine(i);
}
NOTE - "where" statement is considered as filter and "select" statement is considered as projection in LINQ.

4) A same query can be written is following 2 formats-
var query = primes.Where(num => num < 5).Select(num1 => num1);
var q = from p in primes where p < 5 select p;


5) What is the output of following-
var primes = new List<int>() { 1,2,3,8,5,12,78,09,23};
Console.WriteLine(primes.GetType());
Ans - System.Collections.Generic.List`1[System.Int32]
NOTE- variables and objects declared with var are type safe. If we hover hover cursor over variable name , then it will show original type of that variable.


7) What do you mean by Deferred execution in LINQ?
Deferred execution means that the evaluation of an expression is delayed until its realized value is actually required(Source MSDN).In other words it is lazy evaluation of query(LINQ statement). For example -
var primes = new List<int>() { 1, 2, 3, 8, 5, 12, 78, 09, 23 };
var query = primes.Where(num => num < 5).Select(num1 => num1);
foreach (var i in query)
{
Console.WriteLine(i);
}
In the above code query(LINQ statement) wont be evaluated until foreach is run.

8) What will be the output of following program-
  static void Main(string[] args)
        {
            var books = GetBooks();
            var query = from b in books select b.Authors;
            foreach (var qr in query)
            {
                Console.WriteLine(qr);
            }
        }
        private static List<Book> GetBooks()
        {
            var books = new List<Book>()
            {
                new Book{Title = "Title1",Authors = new List<Author>(){new Author{FullName = "Author1"}}},
                new Book{Title = "Title2",Authors = new List<Author>(){new Author{FullName = "Author3"}}}
            };
            return books;
        }
    }
    class Author
    {
        public string FullName { get; set; }
    }
    class Book
    {
        public string Title { get; set; }
        public List<Author> Authors { get; set; }
    }
Ans -
System.Collections.Generic.List`1[LinqTest.Author]
System.Collections.Generic.List`1[LinqTest.Author]
NOTE- People might think it will print Author1 and Author3. But actually the query return the Author object of Book class, which is a string. If you want to print the author name then we will need another foreach loop in the Authors list and print the fullname as well.

9) In the above example how can we print Author full name?
Ans -
var books = GetBooks();
var query = books.SelectMany(book => book.Authors);
//var query = from b in books select b.Authors;
foreach (var qr in query)
{
Console.WriteLine(qr.FullName);
}
SelectMany projects each element of as sequence to an IEnumerable and flattens the resulting sequence into one sequence.

10) What will be output of this code(focus on selectMany) -
static void Main(string[] args)
        {
            var books = GetBooks();
            var query = books.SelectMany(book => book.Authors);
            foreach (var qr in query)
            {
                Console.WriteLine(qr.Address);
            }
        }
        private static List<Book> GetBooks()
        {
            var books = new List<Book>()
            {
                new Book{Title = "Title1",Authors = new List<Author>(){new Author{FullName = "Author1",Address= new List<string>{"Bldg1","Street1"}},new Author{FullName = "Author2",Address= new List<string>{"Bldg2","Street2"}}}},
                new Book{Title = "Title2",Authors = new List<Author>(){new Author{FullName = "Author3",Address= new List<string>{"Bldg3","Street3"}},new Author{FullName = "Author4",Address= new List<string>{"Bldg4","Street4"}}}}
            };
            return books;
        }
    }
    class Author
    {
        public string FullName { get; set; }
        public List<string> Address { get; set; }
    }
    class Book
    {
        public string Title { get; set; }
        public List<Author> Authors { get; set; }
    }
Ans -
System.Collections.Generic.List`1[System.String]
System.Collections.Generic.List`1[System.String]
System.Collections.Generic.List`1[System.String]
System.Collections.Generic.List`1[System.String]

11) How will you print the address of every author in above code?
Ans - Use var query = books.SelectMany(book => book.Authors).SelectMany(author => author.Address);
instead of var query = books.SelectMany(book => book.Authors);

12) If I use following query(replace selectall with select) then what will be the output ?-
var query = books.SelectMany(book => book.Authors).Select(author => author.Address);
Ans -
System.Collections.Generic.List`1[System.String]
System.Collections.Generic.List`1[System.String]
System.Collections.Generic.List`1[System.String]
System.Collections.Generic.List`1[System.String]

No comments:

Post a Comment