Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 循环(IEnumerable)结果并更新,但没有更新?_C#_Linq_Foreach - Fatal编程技术网

C# 循环(IEnumerable)结果并更新,但没有更新?

C# 循环(IEnumerable)结果并更新,但没有更新?,c#,linq,foreach,C#,Linq,Foreach,我正在遍历父记录(“europe”),并用其子记录更新其名为“childPublication”的字段。但是在循环和赋值之后,childPublication为null 这是我的密码: foreach (var e in europe) { string child = e.HasChild ?? ""; if (child.Contains("True")) { IEnumerable<Publication> eChildrens = ch

我正在遍历父记录(“europe”),并用其子记录更新其名为“childPublication”的
字段。但是在循环和赋值之后,childPublication为null

这是我的密码:

foreach (var e in europe)
{
    string child = e.HasChild ?? "";
    if (child.Contains("True"))
    {
        IEnumerable<Publication> eChildrens = children.OfType<Publication>()
                                                .Where(ep => ep.ParentID.Equals(e.PublicationId));

        if (eChildrens.Count() > 0)
        {
            e.ChildPublication = eChildrens;
        }
    }
}              

member.EuropeMiddleEastAfricaPublication = europe;

public class Publication
{
    public int PublicationId { get; set; }
    public int ContentTypeId { get; set; }
    public string PublicationName { get; set; }
    public string PublicationFullName { get; set; }
    public string ShortDescription { get; set; }
    public string LongDescription { get; set; }
    public string URL { get; set; }
    public string CountryId { get; set; }
    public string LanguageId { get; set; }
    public string Active { get; set; }
    public string Subscription { get; set; }
    public string ClientOnly { get; set; }
    public string PrintVersion { get; set; }
    public string EmailVersion { get; set; }
    public string RegisteredforPrint { get; set; }
    public string RegisteredforEmail { get; set; }
    public int ParentID { get; set; }
    public string HasChild { get; set; }
    public IEnumerable<Publication> ChildPublication { get; set; }
}
foreach(欧洲的风险值e)
{
字符串child=e.HasChild??“”;
if(child.Contains(“True”))
{
IEnumerable eChildrens=children.OfType()的子对象
其中(ep=>ep.ParentID等于(e.PublicationId));
如果(eChildrens.Count()>0)
{
e、 儿童出版物=儿童;
}
}
}              
member.europemidleeastafricapublication=欧洲;
公共类出版物
{
public int PublicationId{get;set;}
public int ContentTypeId{get;set;}
公共字符串PublicationName{get;set;}
公共字符串PublicationFullName{get;set;}
公共字符串ShortDescription{get;set;}
公共字符串LongDescription{get;set;}
公共字符串URL{get;set;}
公共字符串CountryId{get;set;}
公共字符串LanguageId{get;set;}
公共字符串活动{get;set;}
公共字符串订阅{get;set;}
公共字符串ClientOnly{get;set;}
公共字符串打印版本{get;set;}
公共字符串版本{get;set;}
公共字符串注册表格式{get;set;}
公共字符串RegisteredforEmail{get;set;}
public int ParentID{get;set;}
公共字符串HasChild{get;set;}
公共IEnumerable子发布{get;set;}
}

您是否尝试过使用
列表或
IEnumerable
。我总是有更多的成功与它的简单收集处理。IEnumerable通常需要定义一个开销稍大的枚举数。

您应该调试您的程序,验证您是否真正进入了
if
并设置了属性。如果不是,那么它将绝对为空。但是请注意,通过关闭循环变量,您正在做一些危险的事情

IEnumerable<Publication> eChildrens = 
  children.OfType<Publication>().Where(ep =>  
                                         ep.ParentID.Equals(e.PublicationId)); 

if (eChildrens.Count() > 0) 
{ 
     e.ChildPublication = eChildrens; 
} 
或者,通过调用
ToList()等方法强制执行查询求值

IEnumerable幼儿=
children.OfType().Where(ep=>
ep.ParentID.Equals(e.PublicationId)).ToList();
如果(eChildrens.Count()>0)
{ 
e、 儿童出版物=儿童;
} 

有关此主题的更多信息,请阅读Eric Lippert的。

首先,您有
eChildren=children
,所以我假设孩子是在某个地方传入的

我可能会将其编码为:

foreach (var e in europe) 
{ 
  // .Net 4.0 use: string.IsNullOrWhiteSpace()
  if (!string.IsNullOrEmpty(e.HadChild)
         // I prefer IndexOf which allows Culture and IgnoreCase 
      && e.HasChild.IndexOf("True", StringComparison.CurrentCultureIgnoreCase))
  { 
    IEnumerable<Publication> eChildrens = 
      children.OfType<Publication>()
              .Where(ep => ep.ParentID.Equals(e.PublicationId))
              .ToList();  //Force the IEnumeration to Enumerate.

    if (eChildrens.Count() > 0) 
    { 
      e.ChildPublication = eChildrens; 
    } 
  } 
}    
foreach(欧洲的风险值e)
{ 
//.NET4.0用法:string.IsNullOrWhiteSpace()
如果(!string.IsNullOrEmpty(e.HadChild)
//我更喜欢IndexOf,它允许文化和无知
&&e.HasChild.IndexOf(“True”,StringComparison.CurrentCultureIgnoreCase))
{ 
i可数儿童=
children.OfType()
其中(ep=>ep.ParentID.Equals(e.PublicationId))
.ToList();//强制执行IEnumeration枚举。
如果(eChildrens.Count()>0)
{ 
e、 儿童出版物=儿童;
} 
} 
}    

问题出在这里。IEnumerable users是针对数据源运行的迭代器。迭代器返回一个新对象,而不是对原始对象的引用


因此,您正在对原始数据的副本进行更改,而不是对实际数据进行更改。如果要修改原始数据,您需要传递一个引用并使用该引用。

您是否验证了您至少最终使用了分配childPublication的代码?需要多少开销?您知道列表以IEnumerable(除其他外)作为基本接口,因此也有一个枚举器吗?感谢您的见解,这是使用列表所必需的问题。
IEnumerable<Publication> eChildrens = 
  children.OfType<Publication>().Where(ep =>  
                                         ep.ParentID.Equals(e.PublicationId)).ToList(); 

if (eChildrens.Count() > 0) 
{ 
  e.ChildPublication = eChildrens; 
} 
foreach (var e in europe) 
{ 
  // .Net 4.0 use: string.IsNullOrWhiteSpace()
  if (!string.IsNullOrEmpty(e.HadChild)
         // I prefer IndexOf which allows Culture and IgnoreCase 
      && e.HasChild.IndexOf("True", StringComparison.CurrentCultureIgnoreCase))
  { 
    IEnumerable<Publication> eChildrens = 
      children.OfType<Publication>()
              .Where(ep => ep.ParentID.Equals(e.PublicationId))
              .ToList();  //Force the IEnumeration to Enumerate.

    if (eChildrens.Count() > 0) 
    { 
      e.ChildPublication = eChildrens; 
    } 
  } 
}