找对象了名单,LT的;元组LT; object1,Object2的> >并存储在视图模型 [英] Get object out of List< Tuple < object1, object2 > > and store in ViewModel

查看:88
本文介绍了找对象了名单,LT的;元组LT; object1,Object2的> >并存储在视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[建议:要阅读以逻辑方式的答案? >>选择TAB [旧的]

 目标:
书presentation与网页相关inventorydetails
如B​​ook.Title,InventoryDe​​tail.Quantity等。
(加入| Book.BookId< = LT; InventoryDe​​tail.BookId)


  

问题1:如何加入


  
  

问题2:如何使用一个元组(元组列表)


  
  

问题3:如何分离的对象(从元组的列表)存储到一个强类型的视图模型


  
  

答1:使用麦克Brind的指导一个可行的方法。


  
  

答2:问题1和2解决!!


  
  

答3:解决问题!! 3


有它的乐趣。我很高兴与大家分享!

 公众的ActionResult指数()
{
 //返回一个元组列表{(WebshopDB.Models.Book,WebshopDB.Models.InventoryDe​​tail)}
 //包含每个元组两个项目:
 //>项目1 {} WebshopDB.Models.Book
 //>项目2 {} WebshopDB.Models.InventoryDe​​tail
 变种tuple_booksinventorydetails = ListOfTuples_BookInventoryDe​​tail(5); //开始正在建设中设置视图模型
 //请参见下面的code的视图模型
  VAR视图模型=新HomeIndexViewModel()
  {
   // //问题书= tuple_books.Contains(书)。??,
   // //问题= InventoryDe​​tail tuple_books.Contains(InventoryDe​​tail)??
  };
 // 结束  返回查看(.....);
}私人列表与LT元组LT;图书,InventoryDe​​tail>> ListOfTuples_BookInventoryDe​​tail(诠释计数)
{
  VAR list_of_tuples =新的List&LT元组LT;图书,InventoryDe​​tail>>();  VAR showbooks = webshopDB.Books
     。加入(webshopDB.InventoryDe​​tails,B => b.BookId,我= GT; i.BookId,(B,I)=>新建{B = B,I = I})
     。凡(O =>(o.b.ShowInWebshop == true)而)
     。凡(O => o.b.BookThumbUrl.Contains(JPG))
     。取(计数);  的foreach(在showbooks VAR项)
  {
    list_of_tuples.Add(Tuple.Create<图书,InventoryDe​​tail>((item.b),(item.i)));
  }
  返回list_of_tuples;
}


解决方案

答(解决方案为我自己贴的问题!)

  //实例添加的元组进行分离对象需要新名单
  清单<图书> T1 =新的List<图书>();
  清单< InventoryDe​​tail> T2 =新的List< InventoryDe​​tail>();  //通过迭代元组和添加每个每个元组的'项目'到新的实例名单
  的for(int i = 0; I< tuple_booksinventorydetails.Count;我++)
  {
    VAR元组= tuple_booksinventorydetails [I]
    //项目1
    T1.Add(tuple.Item1);
    //项目2
    T2.Add(tuple.Item2);
  }  //实例化一个新视图模型来存储分隔列表  // == HomeTupleIndexViewMode类==
  使用系统//;
  使用System.Collections.Generic //;
  使用//<域名> .Models;
  //命名空间和LT;域名> .ViewModels
  // {
  //公共类HomeTupleIndexViewModel
  // {
  //公开名单<图书>图书{搞定;组; }
  //公开名单< InventoryDe​​tail> InventoryDe​​tail {搞定;组; }
  //}
  //}
  // ==  HomeTupleIndexViewModel tupleviewdata =新HomeTupleIndexViewModel()
  {
   书= T1,
   InventoryDe​​tail = 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆