Resizing the Main Window of WPF Applications
C# v.next

LINQ with joins

Dan Wahlin posts about LINQ and Lambdas and Sprocs... Oh My! With his post he defines a complex LINQ query to join several tables from the Northwind database:


public IEnumerable GetOrderDetails(int orderID) {
  NorthwindDataContext db = this.DataContext;
  IEnumerable orderDetails =
    from o in db.Orders
    where o.OrderID == orderID
    join s in db.Shippers on o.ShipVia equals s.ShipperID
    join od in db.OrderDetails on o.OrderID equals od.OrderID
    join p in db.Products on od.ProductID equals p.ProductID
    join supplier in db.Suppliers on p.SupplierID equals supplier.SupplierID
    let total = od.Quantity * od.UnitPrice
    select new OrderDescription
    {
      Product = p.ProductName,
      Quantity = od.Quantity,
      ShipperName = s.CompanyName,
      Total = total,
      UnitPrice = od.UnitPrice,
      SupplierName = supplier.CompanyName
    };
  return orderDetails;
}


Instead of using joins with the LINQ query expression it is also possible to use compound from and access shipper and supplier (with a * to 1 relation) directly:

public IEnumerable GetOrderDetails(int orderID)
{
  NorthwindDataContext db = this.DataContext;
  IEnumerable orderDetails =
    from o in db.Orders
    where o.OrderID == orderID
    from od in o.OrderDetails
    let total = od.Quantity * od.UnitPrice
    select new OrderDescription
    {
      Product = od.Product.ProductName,
      Quantity = od.Quantity,
      ShipperName = o.Shipper.CompanyName,
      Total = total,
      UnitPrice = od.UnitPrice,
      SupplierName = od.Product.Supplier.CompanyName
    };
  return orderDetails;
}

I think the LINQ query with compound from is the simpler one.

Christian

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

Cheap Ed Hardy

I found it thank you very much.Very happy to see your article, I very much to like and agree with your point of view. Thank you for sharing.

Ed Hardy Clothing

I found this site very informative.Ready to hear more in the future.thank you for share.

clarion hotel san jose

Hi Everybody, Your post is indeed highly useful. I promise it won't let you down! So don't miss it!

celebrex 200mg

I really enjoyed this great post and you are very talented and interesting. I really appreciate this excellent post.

buy nexium

Took me time to read All the comments, but i really enjoyed the article. It Proved to Be Very useful to me and I am sure To All the commenters here! It's Always Nice When You Can Not Only Be Informed, entertained goal Assisi! I'm Sure You Had joy writing this article.

hobbies logo

Thanks a lot for enjoying this beauty blog with me. I am appreciating it very much! Looking forward to another great blog. Good luck to the author! all the best!


Hobbies Logo

Monster LadyGaga

Compare to Studio, I prefer to SOLO HD, http://cheapmonsterbeats.us/

for Studio seems large and heavy don’t match to our Asians figure,

the most important is it will generate burden to head if wear it for

a long time. What’s more, Justin Bieber become the spokesmen for

JUST BEATS SOLO http://monsterbeatsstudio.us

Customize Facebook Fan Page

Awesome blog! I will for sure drop by it more often!

buy celebrex

Nice stuff you got, very interesting to read.Well, I do have also in my sleeves, if you have

time don't forget to visit

collarless mens shirt

"This is one of the highly informatics and attractive blogs that has not only educated also informed me in a very effective manner. There are very few blog like this one I have read.I was very pleased to find this site. I wanted to thank you for this great read!! This is a very informative post, it helps me more.

Bollywood sarees

"this image appears during startup while the app is loading in the background. Upload any image the the system automaticlly sizes the image to fit the screen."

Obd2 Scanner Tool

VOLVO DICE 2011 Diagnostic Kommunikationsausrüstung csh

The comments to this entry are closed.