找对象了名单,LT的;元组LT; object1,Object2的&GT; &GT;并存储在视图模型 [英] Get object out of List< Tuple < object1, object2 > > and store in ViewModel
问题描述
[建议:要阅读以逻辑方式的答案? >>选择TAB [旧的]
目标:
书presentation与网页相关inventorydetails
如Book.Title,InventoryDetail.Quantity等。
(加入| Book.BookId&LT; = LT; InventoryDetail.BookId)
问题1:如何加入
问题2:如何使用一个元组(元组列表)
问题3:如何分离的对象(从元组的列表)存储到一个强类型的视图模型
答1:使用麦克Brind的指导一个可行的方法。
答2:问题1和2解决!!
答3:解决问题!! 3
块引用>有它的乐趣。我很高兴与大家分享!
公众的ActionResult指数()
{
//返回一个元组列表{(WebshopDB.Models.Book,WebshopDB.Models.InventoryDetail)}
//包含每个元组两个项目:
//&GT;项目1 {} WebshopDB.Models.Book
//&GT;项目2 {} WebshopDB.Models.InventoryDetail
变种tuple_booksinventorydetails = ListOfTuples_BookInventoryDetail(5); //开始正在建设中设置视图模型
//请参见下面的code的视图模型
VAR视图模型=新HomeIndexViewModel()
{
// //问题书= tuple_books.Contains(书)。??,
// //问题= InventoryDetail tuple_books.Contains(InventoryDetail)??
};
// 结束 返回查看(.....);
}私人列表与LT元组LT;图书,InventoryDetail&GT;&GT; ListOfTuples_BookInventoryDetail(诠释计数)
{
VAR list_of_tuples =新的List&LT元组LT;图书,InventoryDetail&GT;&GT;(); VAR showbooks = webshopDB.Books
。加入(webshopDB.InventoryDetails,B =&GT; b.BookId,我= GT; i.BookId,(B,I)=&gt;新建{B = B,I = I})
。凡(O =&GT;(o.b.ShowInWebshop == true)而)
。凡(O =&GT; o.b.BookThumbUrl.Contains(JPG))
。取(计数); 的foreach(在showbooks VAR项)
{
list_of_tuples.Add(Tuple.Create&LT;图书,InventoryDetail&GT;((item.b),(item.i)));
}
返回list_of_tuples;
}
解决方案答(解决方案为我自己贴的问题!)
//实例添加的元组进行分离对象需要新名单
清单&LT;图书&GT; T1 =新的List&LT;图书&GT;();
清单&LT; InventoryDetail&GT; T2 =新的List&LT; InventoryDetail&GT;(); //通过迭代元组和添加每个每个元组的'项目'到新的实例名单
的for(int i = 0; I&LT; tuple_booksinventorydetails.Count;我++)
{
VAR元组= tuple_booksinventorydetails [I]
//项目1
T1.Add(tuple.Item1);
//项目2
T2.Add(tuple.Item2);
} //实例化一个新视图模型来存储分隔列表 // == HomeTupleIndexViewMode类==
使用系统//;
使用System.Collections.Generic //;
使用//&LT;域名&GT; .Models;
//命名空间和LT;域名&GT; .ViewModels
// {
//公共类HomeTupleIndexViewModel
// {
//公开名单&LT;图书&GT;图书{搞定;组; }
//公开名单&LT; InventoryDetail&GT; InventoryDetail {搞定;组; }
//}
//}
// == HomeTupleIndexViewModel tupleviewdata =新HomeTupleIndexViewModel()
{
书= T1,
InventoryDetail = T2
};
返回查看(tupleviewdata);[Suggestion: Want to read the answers in a logical manner ?? >> choose TAB [Oldest]
Goal: Presentation of books with related inventorydetails on homepage such as Book.Title, InventoryDetail.Quantity etc. (Join|Book.BookId <=< InventoryDetail.BookId)
Problem 1: How to join
Problem 2: How to use a tuple (list of tuples)
Problem 3: How to store the separated objects (from the list of tuples) into a Strongly Typed ViewModel
Answer 1: An possible approach using Mike Brind's Guidance
Answer 2: Problem 1 and 2 tackled !!
Answer 3: Problem 3 tackled !!
Have fun with it. I'm happy to share!!!
public ActionResult Index() { // Return a list of tuples {(WebshopDB.Models.Book, WebshopDB.Models.InventoryDetail)} // Each tuple containing two items: // > Item1 {WebshopDB.Models.Book} // > Item2 {WebshopDB.Models.InventoryDetail} var tuple_booksinventorydetails = ListOfTuples_BookInventoryDetail(5); // BEGIN UNDER CONSTRUCTION Setting up ViewModel // See below the code for the ViewModel var viewmodel = new HomeIndexViewModel() { // Problem // Book = tuple_books.Contains(Book).??, // Problem // InventoryDetail = tuple_books.Contains(InventoryDetail).?? }; // END return View( ..... ); } private List<Tuple<Book, InventoryDetail>> ListOfTuples_BookInventoryDetail(int count) { var list_of_tuples = new List<Tuple<Book, InventoryDetail>>(); var showbooks = webshopDB.Books .Join(webshopDB.InventoryDetails, b => b.BookId, i => i.BookId, (b, i) => new { b = b, i = i }) .Where(o => (o.b.ShowInWebshop == true)) .Where(o => o.b.BookThumbUrl.Contains(".jpg")) .Take(count); foreach (var item in showbooks) { list_of_tuples.Add( Tuple.Create<Book, InventoryDetail>( (item.b), (item.i) ) ); } return list_of_tuples; }
解决方案Answer ( Solution for my own posted question! )
// Instantiate new lists needed to add the to be separated objects of the tuple List<Book> T1 = new List<Book>(); List<InventoryDetail> T2 = new List<InventoryDetail>(); // Iterate through the tuple and add each 'item' of each tuple into the new instantiated lists for (int i = 0; i < tuple_booksinventorydetails.Count; i++) { var tuple = tuple_booksinventorydetails[i]; // Item1 T1.Add(tuple.Item1); // Item2 T2.Add(tuple.Item2); } // Instantiate a new viewmodel to store the separated lists // ==HomeTupleIndexViewMode Class== //using System; //using System.Collections.Generic; //using <Domain>.Models; //namespace <Domain>.ViewModels //{ // public class HomeTupleIndexViewModel // { // public List<Book> Book { get; set; } // public List<InventoryDetail> InventoryDetail { get; set; } // } //} // == HomeTupleIndexViewModel tupleviewdata = new HomeTupleIndexViewModel() { Book = T1, InventoryDetail = T2 }; return View(tupleviewdata);
这篇关于找对象了名单,LT的;元组LT; object1,Object2的&GT; &GT;并存储在视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!