Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Wednesday, 26 June 2013
( Linq | LINQPad )

http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

Use LINQPad

Any()

// Countries which don't have Any people
var counte = Countries.Where(c => !c.Persons.Any());.Dump();

Do Any of the arguments satisfy the condition?

string[] words = { "bob", "dave", "bill", "save" };

// returns true
words.Any(w => w.Contains("av")).Dump();

// returns dave and save
words.Where(w => w.Contains("av")).Dump();

All()

Evaluates the argument against All elements in the list

var people = Persons.Dump();

// Countries where Any of its people have a name with o in it
var countries = Countries.Where(c => c.Persons.Any(
                                                    p => p.Name.Contains("o")
                                                  )
                                )
.Dump();

// Countries where All of its people have a name with o in it
var countr = Countries.Where(c => c.Persons.All(
                                                    p => p.Name.Contains("o")
                                                  )
                                )
.Dump();

image

**GOTCHA**

// Get countries where All the people have a name which contains d
// however this returns the country which has no people
var countrb = Countries.Where(cty => cty.Persons.All(
                                                    p => p.Name.Contains("d")
                                                )
                                    // need to say that the country has people in it
                                    && cty.Persons.Any()
                             );.Dump();

If USA has non people in it, then need Persons.Any to not bring it back.

Select()

// Person where its Countries where All of its people have a name with o in it
var countra = Countries.Where(c => c.Persons.All(
                                                    p => p.Name.Contains("o")
                                                  )
                                )
                       .Select(c => c.Persons )
.Dump();

image

| | # 
# Tuesday, 28 May 2013
( Linq | LINQPad )

Using a block style approach http://stackoverflow.com/questions/2924490/best-ways-to-format-linq-queries

void Main()
{
    var listAccounts = new List<Accounts>();
    
    foreach (var groupAccountLink in GroupAccountLinks)
    {
        var x = Accounts.Where(
                            acc => acc.GroupAccountLinks.Any(
                                                            gal => gal.GroupAccountLinkID == groupAccountLink.GroupAccountLinkID
                                                                   && gal.ReviewItems.Any(
                                                                                        ri => ri.ReviewItemStatusID == 3
                                                                                              && ri.Review.ReviewerID == 1000)
                                                            )
                            && acc.AccountID == 1002 //Charlie
                         ).FirstOrDefault();
        listAccounts.Add(x);
    }

    listAccounts.Dump();
}

Using LINQPad

| | # 
( Linq | LINQPad )
  • Start LINQPad
  • In VS, open Debug -> Attach to Process...
  • Choose LINQPad.exe
  • Set breakpoint in your code called by LINQPad C# code, or write Debugger.Break() in your LINQPad script where you want the debugger to halt.
  • Execute the script from LINQPad and watch the magic.

Can see the code in VS and step through. http://stackoverflow.com/questions/5564369/how-to-debug-linqpad-query-in-visual-studio-debugger

| | #