【问题标题】:Unable to concatenate two IEnumerable lists [duplicate]无法连接两个 IEnumerable 列表 [重复]
【发布时间】:2018-06-29 04:16:29
【问题描述】:

我正在创建一个WEB API。我有两个IEnumerable 列表。最后我想连接它们。

IEnumerable result;
IEnumerable prodDetails = new List<tj_xhqd>();
IEnumerable mainDetails= new List<tj_xhqd>();
int prodInterval, prodCount = 0;
int mainInterval, mainCount = 0;

prodCount = giveProdCount(msn, dt);

if(prodCount==0)
{
    prodDetails = "";
}
else if (prodCount<=500)
{
    prodDetails =  mdcEntitites.tj_xhqd
        .Where( m => (m.zdjh == msn) && (m.sjsj >= dt) )
        .Select( x => new { MSN = x.zdjh, PingDateTime = x.sjsj, PingValue = x.xhqd } )
        .ToList();
}
else
{
    prodInterval = prodCount / 500;

    prodDetails = mdcEntitites.tj_xhqd
        .AsNoTracking()
        .Where( m => (m.zdjh == msn) && (m.sjsj >= dt) )
        .AsEnumerable()
        .Select( (x, i) => new { MSN = x.zdjh, PingDateTime = x.sjsj, PingValue = x.xhqd, i = i } )
        .Where( x => x.i % prodInterval == 0 )
        .ToList();
}

mainCount = giveMainCount(msn, dt);

if(mainCount==0)
{
    mainDetails = "";
}
else if (mainCount <=500)
{
     mainDetails = kesc.tj_xhqd
        .Where(m => (m.zdjh == msn) && (m.sjsj >= dt))
        .Select(x => new { MSN = x.zdjh, PingDateTime = x.sjsj, PingValue = x.xhqd })
        .ToList();
}
else
{
    mainInterval = mainCount / 500;

    mainDetails = kesc.tj_xhqd
        .AsNoTracking()
        .Where(m => (m.zdjh == msn) && (m.sjsj >= dt))
        .AsEnumerable()
        .Select((x, i) => new { MSN = x.zdjh, PingDateTime = x.sjsj, PingValue = x.xhqd, i = i })
        .Where(x => x.i % mainInterval == 0)
        .ToList();
}

if(prodDetails.ToString() == "")
{
    result = mainDetails;
}
else if(mainDetails.ToString()=="")
{
   result = prodDetails;
}
else
{
    result = prodDetails.Concat( mainDetails ); // here I am getting error
}

错误是

cannot convert from 'System.Collections.IEnumerable' to 'System.Collections.Generic.IEnumerable<System.Collections.IEnumerable>'

我怎样才能摆脱这个错误?

任何帮助将不胜感激。

【问题讨论】:

  • 你能用 AddRange() 代替 Concat() 吗?
  • 将 result、prodDetails 和 mainDetails 从声明为 IEnumerable 更改为声明为 IEnumerable
  • prodDetails = ""; 此行无法编译。你发布了你的实际代码吗?
  • @MrFaisal 请将您问题中的所有代码替换为可编译的实际代码 - 因为您发布的代码由于大量输入错误而根本无法编译。
  • 用该匿名类的具体实现重写它。

标签: c# linq asp.net-web-api2 ienumerable


【解决方案1】:

这是将 IEnumerable 列表 A 和 B 合并到 C 中的示例程序。 希望这会有所帮助。

IEnumerable<int> A = new List<int>() { 1, 2, 3 };
IEnumerable<int> B = new List<int>() { 4, 5, 7 };
IEnumerable<int> C = A.Union(B);

【讨论】:

  • Concat 不是问题,请仔细阅读问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-09
  • 2023-04-10
  • 1970-01-01
相关资源
最近更新 更多