简体   繁体   English

LINQ foreach循环中的System.Linq.Enumerable + d__3a`1 [System.String]错误

[英]System.Linq.Enumerable+d__3a`1[System.String] error in LINQ foreach loop

I have a list that I am trying to populate from another list in which I want to combine some data and eliminate some. 我有一个列表,我试图从另一个列表填充,我想要结合一些数据,并消除一些。

In the original list the data looks like this: 在原始列表中,数据如下所示:

 Id     Text      Description

  1      Apple     Red Delicious
  1      Apple     Green
  1      Apple     Granny 
  2      Tomato    Roma
  2      Tomato    Cherry   

I want to condense this information in the second list to look like this: 我想在第二个列表中压缩此信息,如下所示:

  Id    Text     Description
  1     Apple    Red Delicious, Green, Granny
  2     Tomato   Roma, Cherry 

My class object is declared like this: 我的类对象声明如下:

 [Serializable]
 public class Food
 {
     public int Id { get;set; }
     public string Text { get;set; }
     public string Description { get;set; } 
 }

So I want to loop through the old list and this is how I'm trying to do it in the code: 所以我想循环遍历旧列表,这就是我在代码中尝试这样做的方式:

var ids = oldList.Select(i => i.Id).Distinct(); //get distinct list of ids (e.g. 1,2)
List<Food> newList = ids.Select(i => new Food(){
   Id = i,
   Text = oldList.Where(o => o.Id == i).Select(o => o.Text).Take(1).ToString(),
   Description = string.Join(",", oldList.Where(o => o.Id == i).Select(o => o.Description).ToString())
}).ToList();

Right now I'm getting the System.Linq.Enumerable+d__3a`1[System.String] error because of .Take(), but if I change it just .ToString(), I get a slightly different error but from the same source System.linq.Enumerable, if I do FirstOrDefault() same thing, .Distinct() same thing. 现在我因为.Take()而得到System.Linq.Enumerable + d__3a`1 [System.String]错误,但是如果我只改变它.ToString(),我会得到一个略有不同的错误但是来自同一个source System.linq.Enumerable,如果我做FirstOrDefault()同样的东西,.Distinct()同样的事情。

I think I understand that the problem is that for Text and Description its returning IEnumerable for Text and Description so I'm not getting the actual values that I want and I'm not sure I understand how to correctly convert it .ToList(). 我想我明白问题是,对于Text和Description,它返回IEnumerable for Text和Description所以我没有得到我想要的实际值,我不确定我是否理解如何正确转换它.ToList()。 What is the correct way to access these values? 访问这些值的正确方法是什么?

You're calling ToString() on the result of Take and Select - and that's not going to do anything nice. 你在TakeSelect的结果上调用了ToString() - 这不会做任何好事。 It's not clear why you're calling ToString explicitly in Description , and for Text you really want FirstOrDefault or First instead, as you just want the first result: 目前尚不清楚为什么你在Description显式调用ToString ,而对于Text你真的想要FirstOrDefaultFirst ,因为你只想要第一个结果:

List<Food> newList = ids.Select(i => new Food {
   Id = i,
   Text = oldList.Where(o => o.Id == i).Select(o => o.Text).FirstOrDefault(),
   Description = string.Join(",", oldList.Where(o => o.Id == i)
                                         .Select(o => o.Description))
}).ToList();

Basically, calling ToString() on a sequence ( IEnumerable<T> ) is almost never appropriate. 基本上,在调用ToString()上的序列( IEnumerable<T>是几乎从未适当。

This is typical job for GroupBy operator: 这是GroupBy运营商的典型工作:

var newList = oldList.GroupBy(x => x.Id, 
                        (key, values) => 
                           new Food() {
                             Id = key,
                             Text = values.First().Text,
                             Description = string.Join(", ", 
                                              values.Select(v => v.Description))
                            })
                     .SelectMany(x => x)
                     .ToList();

I think that you are perhaps overcomplicating your approach. 我认为你可能会使你的方法过于复杂。

This is a very roughly hacked-together sample (you can run it in LinqPad if you like), but it uses .GroupBy to solve the same problem... 这是一个非常粗略的黑客攻击样本(如果你愿意,你可以在LinqPad中运行它),但是它使用.GroupBy来解决同样的问题......

var l = new List<Tuple<int, string, string>>();

l.Add(Tuple.Create(1, "Apple", "Red Delicious"));
l.Add(Tuple.Create(1, "Apple", "Green"));
l.Add(Tuple.Create(1, "Apple", "Granny"));
l.Add(Tuple.Create(2, "Tomato", "Roma"));
l.Add(Tuple.Create(2, "Tomato", "Cherry"));

var res = l.GroupBy(t => t.Item2).Select(g => new { Id = g.First().Item1, Text = g.Key, Description = string.Join(", ", g.Select(i => i.Item3)) });
res.Dump(); // Linqpad output

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 出现错误-&gt; System.Linq.Enumerable + WhereSelectListIterator`2 [Tdsb.Aris.Pims.Entity.PartnershipFunding,System.String] - Getting error -> System.Linq.Enumerable+WhereSelectListIterator`2[Tdsb.Aris.Pims.Entity.PartnershipFunding,System.String] 对Xdocument的Linq查询返回“System.linq.Enumerable + WhereSelectEnumerableIterator&#39;2 [system.XML.Linq.Xelement,System.String] - Linq query on Xdocument returns "System.linq.Enumerable+WhereSelectEnumerableIterator'2[system.XML.Linq.Xelement,System.String] System.Linq.Enumerable出错。 <TakeIterator> d__3a`1.MoveNext() - Error on System.Linq.Enumerable.<TakeIterator>d__3a`1.MoveNext() 意外错误:LINQ to Entities 无法识别方法“System.String DecryptValue(Byte[], System.String)”方法 - Unexpected Error : LINQ to Entities does not recognize the method 'System.String DecryptValue(Byte[], System.String)' method LINQ to Entities 无法识别“System.String Decrypt(System.String, System.String)”方法 - LINQ to Entities does not recognize the method 'System.String Decrypt(System.String, System.String)' method LINQ to Entities无法识别方法&#39;System.String getFullname(System.String,System.String)&#39; - LINQ to Entities does not recognize the method 'System.String getFullname(System.String, System.String)' LINQ 到实体无法识别 System.String - LINQ to Entities Not recognize System.String 方法系统linq的类型参数可枚举为可枚举-错误 - the type arguments for method system linq enumerable as enumerable - Error Linq-收到错误“无法将类型为“ System.String”的对象转换为类型为“ System.Byte []”。” - Linq - Receiving error “Unable to cast object of type 'System.String' to type 'System.Byte[]'.” System.Linq.Enumerable + WhereSelectListIterator`2 - System.Linq.Enumerable+WhereSelectListIterator`2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM