Linq To XML : Check for existence of element

var bookQuery = 

from book in bookXml.Descendants("Item")
let attributes = book.Element("ItemAttributes")

let price = Decimal.Parse((
  book.Elements("OfferSummary").Any() 
  && book.Element("OfferSummary").Elements("LowestNewPrice").Any()
  ? book.Element("OfferSummary")
    .Element("LowestNewPrice")
    .Element("Amount").Value
  : (attributes.Elements("ListPrice").Any()
      ? attributes.Element("ListPrice").Element("Amount").Value 
      : "0"))) / 100

select new {Price = price};

Elements(“node”).Any() does the trick.

Some other Linq tips: Some extensions to use with Linq include: Intersect, Union and Except. Intersect returns a IEnumerable collection which is a inner join between the 2 collections. Union returns a collection which is a full outer join between the 2 collections Except returns elements unique to collection1 that don’t exist in the collection2.

Method Hiding... Polymorphism in C#

In the following code:

interface IBand
{
int ID {get;set;}
string Name {get;set;}
void GetStatus();
}
public class Band : IBand
{
public int ID { get; set; }
public string Name { get; set; }

public Band()
{
GetStatus();
}
public void GetStatus()
{
ID = 555;
Name = "Ring";
Console.WriteLine("Base Class Called");
}
}

public class ABand : Band, IBand
{
public string Update { get; set; }

public ABand() : base() {}

public new void GetStatus()
{
ID = 655;
Name = "TEsst";
Update = "ddd";
Console.WriteLine("Derived class Called");
}
}
class Program
{
static void Main(string[] args)
{
IBand band = new ABand();
band.GetStatus();
}

}

We have 2 classes, Band and ABand which implement the IBand interface explicitly.

Now the call to band.GetStatus in Main will display "Derived class called". This will only happen if the Derived class (ABand) also implements the interface IBand explicitly (not in the literal sense in that it does not implement the methods using the IBand.GetStatus syntax)

If the ABand class does not implement the IBand interface explicitly (but implicitly since it inherits from Band which implements IBand) then the above code will output "Base class called"

However note one thing, that when the CTor for ABand is invoked it will invoke the ctor Band class and which will call GetStatus() method from within Band class hence this will output "Base class called" even though the ABand class has implemented the IBand interface explicilty.

Passing state to threads via anonymous calls

We already know that we can pass state into to Threads in 2 ways:

1. using ParameterizedThreadStart delegate

2.using global variables(reference or value types) accessible to the main thread as well the instantiated thread. This is also the way to share data amonst multiple threads. Infact the most common way to share data between threads is using static variables where application wide scope is desired.

3.using Thread.QueueUserWorkItem ... This differs from Parameterized ThreadStart delegate since it uses threads from a preconfigured ThreadPool instead of manually creating threads

There is another cool method to pass information to threads is via anonymous methods. For e.g.
static void Main
{
    string mainMessage = "Hello from Main !!!";
    Thread t = new Thread(delegate() { Print(mainMessage);});
    t.Start();
}
static void Print(string message)
{
    Console.Writeline(message);
}
Advantage of using this technique is that, it provides strongly typed access to the parameters passed to the thread. The Target Method can accept any number of arguments, and no casting is required (unlike Parameterized Start where the passed state is an “object”) . The flip side, though, is that you must keep outer-variable semantics in mind
Twitter