将两个列表合并到一个列表中 [英] merge two lists in a single list

查看:96
本文介绍了将两个列表合并到一个列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用asp.nt mvc 5,linq



i有两张桌子

订单

付款





列表<订单> order =  new  List< Order>(repository.Get())。ToList()); 


列表<支付> payment = new 列表< Payment>(repository.Get())。ToList());

List< orderPaymentViewModel> ordrPaymt = new List< orderPaymentViewModel>();
ordrPaymt = order.AddRange(payment);

return view(Tuple.Create(ordrPaymt));





i希望将这两个列表传递给视图,然后想要使用一个freach迭代



ie

 @ foreach( var  item  in  Model.item1)
{
orderData paymentData
1 1
2 2
3 3


}



数据之间没有关系...主要要求是显示它们并排说两个colmns

解决方案

将两个列表传递给视图非常简单。您可以使用ViewBag,也可以只创建具有两个列表的自定义或动态模型。就是这样。



但问题是你打算如何遍历这两个。正如BillWoodruff所问,你可能在这两者之间有一些关系。此时我不会将两个列表传递给视图 - 只有当您想在一个网页上显示存储库中的所有数据时(我怀疑您是否想要这样)。如果您想要过滤视图中的数据,并使用您可能拥有的关系,那么您已经引入了巨大的开销 - 这不是视图的关注点!它再次是MVC的概念。你还必须考虑,例如EF返回IEnumerables,在遍历它们之前不会枚举它们。从中列出列表意味着您将所有数据读入内存。这在生产场景中是一个巨大的错误。只有在你真的需要的情况下才能通过列表,或者在每种情况下都是短的。



我不知道你的存储库是什么。我建议使用EF和导航器的可能性,你可以简单地管理和导航模型实体之间的关系。同时你可以使用延期执行。



[基于下面的聊天更新]

有一种方法你可以用来并行遍历两个IEnumerables。您可以评估它并适应您的需求。



 IEnumerable orders =  new   int  [] { 1  2  3  4 }; 
IEnumerable payments = new int [] { 30 31 32 33 34 35 };

var oe =(IEnumerator)orders.GetEnumerator();
var pe =(IEnumerator)payments.GetEnumerator();

bool no,np;
while ((no = oe.MoveNext())|(np = pe.MoveNext()))
{
Console .WriteLine( String .Format( < tr> < td> {0}< / td>< td> {1}< / td>< / tr>
no?(( int )oe.Current).ToString(): & nbsp;
np?(( int )pe.Current).ToString(): & nbsp;
));

}


合并2个列表的方法很少:

1)你可以使用< a href =http://msdn.microsoft.com/en-us/library/vstudio/bb909044%28v=vs.90%29.aspx>联盟方法 [ ^ ]



2)你可以加入两个列表 [ ^ ]通过linq查询。





< b>

这是一个简单的例子:

  void  Main()
{
List< Order> orders = new 列表<订单> {
new 订单( 31 ),
订单( 32 ),
new 订单( 33 ),
new 订单( 34 )};

列表<支付> payment = new 列表<支付> {
付款( 1 30 ),
付款( 2 31 ),
付款( 3 32 ),
付款( 4 33 ),
new 付款( 5 34 ),
付款( 6 35 )};

var qry = 来自 o in 订单
加入 p 支付o.OrderId等于p。 OrderId
选择 new {o.OrderId,p.PaymentId};

foreach var op in qry)
{
Console.WriteLine( OrderId = {0} ; PaymentId = {1},op.OrderId,op.PaymentId);
}
}

// 在此定义其他方法和类
public class 订单
{
private int orderid = 0 ;
// 构造函数
public 订单( int ordid)
{
orderid = ordid;
}

public int OrderId
{
get
{
return orderid;
}
set
{
orderid = value ;
}
}
}

public class 付款
{
私有 int paymentid = 0 ;
private int orderid = 0 ;

public 付款( int payid, int ordid)
{
paymentid = payid;
orderid = ordid;
}

public int PaymentId
{
get
{
return paymentid;
}
set
{
paymentid = value ;
}
}

public int OrderId
{
get
{
return orderid;
}
set
{
orderid = value ;
}
}
}



结果:

 OrderId = 31; PaymentId = 2 
OrderId = 32; PaymentId = 3
OrderId = 33; PaymentId = 4
OrderId = 34; PaymentId = 5


I am using asp.nt mvc 5 ,linq

i have a two tables
order
payment


    List<Order> order=new List<Order>(repository.Get()).ToList());
      

     List<Payment> payment=new List<Payment>(repository.Get()).ToList());

      List<orderPaymentViewModel> ordrPaymt = new List<orderPaymentViewModel>();
             ordrPaymt =order.AddRange(payment);

return view(Tuple.Create(ordrPaymt));



i want to pass these two lists to a view and then want to iterate using a freach

i.e

@foreach(var item in Model.item1)
{
orderData   paymentData
1              1
2              2
3              3


}


there is no relation between data..the main requirement is to display them side by side in say two colmns

解决方案

Passing two lists to a view is really easy. You can use the ViewBag or you can simply create a custom or dynamic model having the two list. That's it.

But the question is how you intend to traverse these two. As BillWoodruff asked, you probably have some relations between these two. And at that point I wouldn't pass two lists to the view - only if you want to show all the data in your repository on one web page (I doubt you want this). If by any chance you want to filter the data in the view, and use the relation you might have, you have introduced a huge overhead - this is not the concern of the view! It is agains MVC concepts. You have to consider also, that EF for example is returning IEnumerables, which are not enumerated until you traverse them. Making lists out of them means that you read all the data into memory. This is a huge mistake in a production scenario. Pass lists only if you really need to, or if they are short ones in every case.

I don't know what you repository is. I would recommend using EF and the navigator possibilities which it has, sou you can simply manage and navigate trough the relations between the entities of your model. And in the same time you can make use of the deferred execution.

[Update based on the chat below]
There is a method you can use to traverse two IEnumerables in parallel. You can evaluate it and adapt to your needs.

IEnumerable orders = new int[] {1,2,3,4};
IEnumerable payments = new int[] {30,31,32,33,34,35};
 
var oe = (IEnumerator)orders.GetEnumerator();
var pe = (IEnumerator)payments.GetEnumerator();
 
bool no, np;
while((no = oe.MoveNext()) | (np = pe.MoveNext()))
{
Console.WriteLine(String.Format("<tr><td>{0}</td><td>{1}</td></tr>", 
no ? ((int)oe.Current).ToString() : "&nbsp;",
np ? ((int)pe.Current).ToString() : "&nbsp;"
));
 
}


There is few ways to merge 2 lists:
1) you can use Union method[^]
or
2) you can join two list[^] via linq query.


[Edit]
Here is simple example:

void Main()
{
	List<Order> orders = new List<Order> {
			new Order(31),
			new Order(32),
			new Order(33),
			new Order(34)};

	List<Payment> payments = new List<Payment>  {
			new Payment(1, 30),
			new Payment(2, 31),
			new Payment(3, 32),
			new Payment(4, 33),
			new Payment(5, 34),
			new Payment(6, 35)};

	var qry = from o in orders
		join p in payments on o.OrderId equals p.OrderId
		select new{o.OrderId, p.PaymentId};
	
	foreach(var op in qry)
	{
		Console.WriteLine("OrderId={0}; PaymentId={1}", op.OrderId, op.PaymentId);
	}
}

// Define other methods and classes here
public class Order
{
	private int orderid = 0;
	//constructor
	public Order(int ordid)
	{
		orderid = ordid;
	}
	
	public int OrderId
	{
		get
		{
			return orderid;
		}
		set
		{
			orderid = value;
		}
	}
}

public class Payment
{
	private int paymentid = 0;
	private int orderid = 0;
	
	public Payment(int payid, int ordid)
	{
		paymentid = payid;
		orderid = ordid;
	}
	
	public int PaymentId
	{
		get
		{
			return paymentid;
		}
		set
		{
			paymentid = value;
		}
	}

	public int OrderId
	{
		get
		{
			return orderid;
		}
		set
		{
			orderid = value;
		}
	}
}


Result:

OrderId=31; PaymentId=2
OrderId=32; PaymentId=3
OrderId=33; PaymentId=4
OrderId=34; PaymentId=5


这篇关于将两个列表合并到一个列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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