Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using asp.nt mvc 5 ,linq

i have a two tables
order
payment


C#
    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
C#
@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
Posted
Updated 23-Nov-14 10:24am
v3
Comments
BillWoodruff 23-Nov-14 11:54am    
You have a list of Orders, and a list of Payments: is there a "relationship" of some kind between the items ? If there's no relationship, sure, throw them all in new list, but, if there's a relationship, we need to know what that is and how you wish to preserve that in the new list.
Member 9129971 23-Nov-14 15:50pm    
currenlty there is no relationship
Member 9129971 23-Nov-14 15:52pm    
acutally i want to loop through them using the sinlge loop but for that i need them in one list, but concat,zip,union are not working for me ,so what should i do
Maciej Los 23-Nov-14 14:06pm    
Is there any relationship between data?
Member 9129971 23-Nov-14 15:52pm    
no

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.

C#
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;"
));
 
}
 
Share this answer
 
v2
Comments
Member 9129971 23-Nov-14 16:00pm    
actually i want to display them in lets say for example two columns i.e
@foreach(var item in Model.orderPayment)
orderData paymentData
<tr><td>1</td> <td>1</td></tr>
<tr><td>1</td> <td>1</td></tr>
<tr><td>1</td> <td>1</td></tr>

and that is possible only if i have only 1 list which contains both order and payment data....
Zoltán Zörgő 23-Nov-14 16:06pm    
And how can you tell, which orderData comes aside which paymentData? That is thel relation we are all asking about....
Member 9129971 23-Nov-14 16:18pm    
actually there is no relation means there could be any data that can be displayed in the second column against first,,,in short there is no relation...its just a play of tr td..but i have no way to display this data except to wrap them in a single list and then iterating them
Member 9129971 23-Nov-14 16:13pm    
actually there is no relation means there could be any data that can be displayed in the second column against first,,,in short there is no relation...its just a play of tr td..but i have no way to display this data except to wrap them in a single list and then iterating them
Member 9129971 23-Nov-14 16:16pm    
for example if i have to display the data in differnt rows then i can simply display them using two lists ie two foeach loops but here i have a scenario where i have to dispaly them in a colmn so how i can display data in colmn using two foreach loops...i hope u understand what i want to say
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:
C#
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
 
Share this answer
 
v2
Comments
Member 9129971 23-Nov-14 16:01pm    
have seen them but did not solved my prblem giving me following error:
Error 9 'System.Collections.Generic.List' does not contain a definition for 'Concat' and the best extension method overload 'System.Linq.Queryable.Concat(System.Linq.IQueryable, System.Collections.Generic.IEnumerable)' has some invalid arguments

same for union

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900