开发者

Using tuple technique to join more than two tables (Strongly Typed)

Would it be possible according article

Get object 开发者_StackOverflow社区out of List< Tuple < object1, object2 > > and store in ViewModel

to separate more than two tables using the tuple technique?

Goal: Joining 7 tables (for administration purposes).


Answer: Yes

Solution:

// GET: /Administration/
public ActionResult Index()
{
  //Important Article
  //http://stackoverflow.com/questions/4189730/get-object-out-of-list-tuple-object1-object2-and-store-in-viewmodel
  //Methods that does the trick!!
  var tuple = ListOfTuples_Orders_OrderDetails_Books_Carts_InventoryDetails_Authors_Genres();

  // Instantiate new lists needed to add the 'to be' separated objects of each tuple
  // Be aware of the objects order (keep in mind!!!) ....
  List<Book> T1 = new List<Book>(); 
  List<InventoryDetail> T2 = new List<InventoryDetail>();
  List<Order> T3 = new List<Order>();
  List<OrderDetail> T4 = new List<OrderDetail>();
  List<Cart> T5 = new List<Cart>();
  List<Author> T6 = new List<Author>();
  List<Genre> T7 = new List<Genre>();

  for (int i = 0; i < tuple.Count; i++)
  {
    var tuple = tuple[i];
    T1.Add(tuple.Item1);
    T2.Add(tuple.Item2);
    T3.Add(tuple.Item3);
    T4.Add(tuple.Item4);
    T5.Add(tuple.Item5);
    T6.Add(tuple.Item6);
    T7.Add(tuple.Item7);
  }

  HomeTupleIndexViewModel tupleviewdata = new HomeTupleIndexViewModel() 
  {         
    Book = T1,
    InventoryDetail = T2,
    Order = T3 ,
    OrderDetail= T4,
    Cart =T5,
    Author =T6,
    Genre =T7,
  };
  return View(tupleviewdata);

  //---------------------------
  //    Intermediate Window
  //---------------------------
  //
  //    tupleviewdata
  //            
  //    {WebshopDB.ViewModels.HomeTupleIndexViewModel}
  //        Author: Count = 2
  //        Book: Count = 2
  //        Cart: Count = 2
  //        Genre: Count = 2
  //        InventoryDetail: Count = 2
  //        Order: Count = 2
  //        OrderDetail: Count = 2
  //---------------------------
}       

private List<Tuple<Book, InventoryDetail, Order, OrderDetail, Cart, Author, Genre>> 
  ListOfTuples_Orders_OrderDetails_Books_Carts_InventoryDetails_Authors_Genres()
{
  var list_of_tuples = new List<Tuple<Book, InventoryDetail, Order, OrderDetail, Cart, Author, Genre>>();
  var accounting = webshopDB
    .Books
    .SelectMany( book => webshopDB.InventoryDetails, (book, inventorydetail) => new { book = book, inventorydetail = inventorydetail } )
    .SelectMany( temp0 => webshopDB.Orders, (temp0, order) => new { temp0 = temp0, order = order } )
    .SelectMany( temp1 => webshopDB.OrderDetails, (temp1, orderdetail) => new { temp1 = temp1, orderdetail = orderdetail } )
    .SelectMany( temp2 => webshopDB.Carts, (temp2, cart) => new { temp2 = temp2, cart = cart } )
    .SelectMany( temp3 => webshopDB.Authors, (temp3, author) => new { temp3 = temp3, author = author } )
    .SelectMany( temp4 => webshopDB.Genres, (temp4, genre) => new { temp4 = temp4, genre = genre } )
    .Where( temp5 => (temp5.temp4.temp3.temp2.temp1.temp0.book.BookId == temp5.temp4.temp3.temp2.temp1.temp0.inventorydetail.BookId ) )
    .Where( temp5 => (temp5.temp4.temp3.temp2.temp1.order.OrderId == temp5.temp4.temp3.temp2.orderdetail.OrderId) )
    .Where( temp5 => (temp5.temp4.temp3.temp2.orderdetail.BookId == temp5.temp4.temp3.temp2.temp1.temp0.book.BookId ) )
    .Where( temp5 => (temp5.temp4.temp3.temp2.temp1.temp0.book.BookId == temp5.temp4.temp3.cart.BookId) )
    .Where( temp5 => (temp5.temp4.temp3.temp2.temp1.temp0.book.AuthorId == temp5.temp4.author.AuthorId) )
    .Where( temp5 => (temp5.temp4.temp3.temp2.temp1.temp0.book.GenreId == temp5.genre.GenreId) )
    .Where( temp5 => (temp5.temp4.temp3.temp2.temp1.temp0.book.ShowInWebshop == true) )
    // I wanted the objects not the 'properties', so for educational reasons the .Select has been commented out ...
    //.Select( temp5 => new { Title = temp5.temp4.temp3.temp2.temp1.temp0.book.Title,
    //                        Price = temp5.temp4.temp3.temp2.temp1.temp0.book.Price,
    //                        Quantity = temp5.temp4.temp3.temp2.temp1.temp0.inventorydetail.Quantity,
    //                        ShowInWebshop = temp5.temp4.temp3.temp2.temp1.temp0.book.ShowInWebshop,
    //                        Name = temp5.temp4.author.Name,
    //                        DateCreated = temp5.temp4.temp3.cart.DateCreated,
    //                        OrderDate = temp5.temp4.temp3.temp2.temp1.order.OrderDate,
    //                        UnitPrice = temp5.temp4.temp3.temp2.orderdetail.UnitPrice } )
    ;  // <<< This 'little buggy thing' can easily be forgotten!!!

        // Creating each tuple needs special care!!!
  foreach (var item in accounting)
  {
    list_of_tuples.Add(Tuple.Create<Book, InventoryDetail, Order, OrderDetail, Cart, Author, Genre>( 
      (item.temp4.temp3.temp2.temp1.temp0.book), 
      (item.temp4.temp3.temp2.temp1.temp0.inventorydetail),
      (item.temp4.temp3.temp2.temp1.order),
      (item.temp4.temp3.temp2.orderdetail),
      (item.temp4.temp3.cart),
      (item.temp4.author),
      (item.genre) ) );
  }
  return list_of_tuples;

  //---------------------------
  //    Intermediate Window
  //---------------------------
  //
  //    list_of_tuples
  //
  //    Count = 2
  //    [0]: {(WebshopDB.Models.Book, 
  //           WebshopDB.Models.InventoryDetail, 
  //           WebshopDB.Models.Order, 
  //         WebshopDB.Models.OrderDetail, 
  //           WebshopDB.Models.Cart, 
  //           WebshopDB.Models.Author, 
  //           WebshopDB.Models.Genre)}
  //    [1]: {(WebshopDB.Models.Book, 
  //           WebshopDB.Models.InventoryDetail, 
  //           WebshopDB.Models.Order, 
  //           WebshopDB.Models.OrderDetail, 
  //           WebshopDB.Models.Cart, 
  //           WebshopDB.Models.Author, 
  //           WebshopDB.Models.Genre)}
  //---------------------------         
}

Next step: Setting up an Index.aspx with controls (.ascx's or partials) >> designing the administration stuff but I guess this won't be a big deal .. I'm happy to share!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜