Reputation: 99
I have a list of objects EnTeteCommande (which each have 3 properties) and a list of strings. I want to concat the elements of these 2 lists couples. For example :
List<EnteteCommande> Ent = new List<List<EnteteCommande>();
Ent.Add(E1); // E1 : { code = 011 , Date = 10/05/2016 , Ref = 01236 }
Ent.Add(E2); // E1 : { code = 012 , Date = 10105/2016 , Ref = 01237 }
List<string> Status = new List<List<string>>();
Status = { "test1" , "test2"}
The result that I want is:
r1 : { code = 011 , Date = 10/05/2016 , Ref = 01236 , statut = "test1"}
r2 : { code = 012 , Date = 10105/2016 , Ref = 01237 , statut = "test2" }
I used linq's Zip but I can't display the result in razor
var historiques = Ent as List<AURES_GROS_EnTeteCommande>;
var statuts = statuts as List<string>;
var resultats = historiques.Zip(statuts, (item, statut) => new { item = item, statut = statut });
ViewBag.resultats = resultats;
and this my code in razor view :
foreach (var it in ViewBag.resultats)
{
<tbody>
<tr class="even pointer">
<td class="a-center ">
<input type="checkbox" class="tableflat" value="" name="ids" id="ids">
</td>
<td> @it.item.Code</td>
<td> @it.item.Date.ToString("MM/dd/yyyy")</td>
<td> @it.item.Ref</a></td>
<td>
@it.statut
</td>
</tbody>
Does anyone have a solution? Thanks.
Upvotes: 2
Views: 729
Reputation: 13399
ent.Zip(status, (e,s) =>new {e.code, e.Date, e.Ref, status = s})
Working sample: https://dotnetfiddle.net/eay1Zw
List<EnteteCommande> Ent = new List<EnteteCommande>();
var E1 = new EnteteCommande { code = "011" , Date = "10/05/2016" , Ref = "01236" };
var E2 = new EnteteCommande { code = "012" , Date = "10105/2016" , Ref = "01237" };
Ent.Add(E1);
Ent.Add(E2);
List<string> Status = new List<string>{ "test1" , "test2"};
var result = Ent.Zip(Status, (e,s) => new {e.code, e.Date, e.Ref, status = s}).ToList();
Upvotes: 3