追加合并两个项目的IEnumerable [英] append two IEnumerable items

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

问题描述

 的IEnumerable<地址> AddressSet1 = myServices.GetAddresses(LocationId1);
IEnumerable的<地址> AddressSet2 = myServices.GetAddresses(LocationId2);

我想上面的两个AddressSets结合

我试过的IEnumerable<地址> AllAddresses = AddressSet1.Concat(AddressSet2)

但毕竟这当我尝试自IEnumerable AllAddresses访问项目通过我的Razor视图

  @if(!myHelper.IsNullorEmpty(model.AllAddresses)
   {
     @ Html.EditorFor(型号=> model.AllAddresses)
   }

和我收到错误 - 路径中。任何建议,非法字符来确定该错误的原因

如果我试图用出的毗连我能看到AddressSet1 / AddressSet2记录显示在页面上运行我的网页。但是当我尝试将二者结合起来,形成我可枚举AllAddresses,它是引发错误,请帮助

下面粘贴

是我的编辑模板

  @model MyServiceRole.Models.Addresses
@ {
    ViewBag.Title =所有地址的项目;    }
<表>
&所述; TR>
 &所述; TD>
 <跨度>指数< / SPAN>   < / TD>
   &所述; TD>< / TR>
 &所述; TR>
 < TD>地址XID< / TD>
 &所述; TD>
@ Html.EditorFor(型号=> model.AddressID)
< / TD>
< / TR>
 &所述; TR>
 < TD>标题< / TD>
 &所述; TD>
@ Html.EditorFor(型号=> model.Title)
< / TD>
< / TR>
&所述; TR>
 < TD>说明与LT; / TD>
 &所述; TD>
 @ Html.EditorFor(型号=> model.Description)
< / TD>
< / TR>
&所述; TR>
 < TD>图片URL< / TD>
 &所述; TD>
 @ Html.EditorFor(型号=> model.Photo.URL)
< / TD>
< / TR>
< /表>


解决方案

我测试您的问题和遇到同样的问题。

 列表<串GT;一个=新名单<串GT; {一};
清单<串GT; B =新的List<串GT; {B};IEnumerable的<串GT; CONCAT = a.Concat<串GT;(二);
的foreach(字符串s的Concat){} //这个作品返回查看(CONCAT);

在视图:

  @model IEnumerable的<串GT;@foreach(字符串s的模型)//这炸毁
{
}
@ Html.EditorFor(M = GT;产品型号)//还鼓起

它看起来像你真的不能使用模板或枚举的

  System.Linq.Enumerable.ConcatIterator< T>

的毗连视图中创建。这似乎是一个错误。

反正加入 .ToList()解决您的问题。

 返回查看(concat.ToList());

IEnumerable<Addresses> AddressSet1=myServices.GetAddresses(LocationId1);
IEnumerable<Addresses> AddressSet2=myServices.GetAddresses(LocationId2);

I want to combine the above two AddressSets

I tried IEnumerable<Addresses> AllAddresses=AddressSet1.Concat(AddressSet2)

But after this when I try to access items from IEnumerable AllAddresses by on my razor view

 @if(!myHelper.IsNullorEmpty(model.AllAddresses )
   {
     @Html.EditorFor(model => model.AllAddresses  )
   }

and I am getting errors -- Illegal characters in path .Any suggestions to identify cause of this error ?

If I am trying to run my page with out the Concat I am able see the records in AddressSet1 /AddressSet2 displayed on the page .But when I try to combine the two to form I Enumerable AllAddresses ,it is throwing errors please help

pasted below is my Editor Template

@model MyServiceRole.Models.Addresses
@{
    ViewBag.Title = "All addresses Items";

    }
<table>
<tr>
 <td>
 <span>Index</span>

   </td>
   <td>

</tr>
 <tr>
 <td>Address XID</td>
 <td>
@Html.EditorFor(model => model.AddressID)
</td>
</tr>
 <tr>
 <td>Title</td>
 <td>
@Html.EditorFor(model => model.Title)
</td>
</tr>
<tr>
 <td>Description</td>
 <td>
 @Html.EditorFor(model => model.Description)
</td>
</tr>
<tr>
 <td>Image URL</td>
 <td>
 @Html.EditorFor(model => model.Photo.URL)
</td>
</tr>
</table>

解决方案

I tested your issue and ran into the same problem.

List<string> a = new List<string>{ "a" };
List<string> b = new List<string>{ "b" };

IEnumerable<string> concat = a.Concat<string>(b);
foreach(string s in concat) { } // this works

return View(concat);

In view:

@model IEnumerable<string>

@foreach(string s in Model)  //This blows up
{
}
@Html.EditorFor(m => Model) //Also blows up

It looks like you honestly can't use templates with or enumerate over the

System.Linq.Enumerable.ConcatIterator<T>

class that Concat creates within a View. This seems like a bug.

Anyway adding .ToList() fixes your issue.

return View(concat.ToList());

这篇关于追加合并两个项目的IEnumerable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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