使用元组技术连接两个以上的表(强类型)

时间:2010-11-19 11:11:40

标签: linq linq-to-sql linq-to-entities

根据文章

是否可行

Get object out of List< Tuple < object1, object2 > > and store in ViewModel

使用元组技术分隔两个以上的表?

目标:加入7个表格(用于管理目的)。

1 个答案:

答案 0 :(得分:0)

答案:是的

解决方案:

// 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)}
  //---------------------------         
}

下一步:使用控件(.ascx或partials)设置Index.aspx&gt;&gt;设计管理的东西,但我想这不会是一个大问题..     我很高兴分享!