LINQ 填充范围
我不知道如何使用 LINQ 表达式执行第二部分(for/foreach),并且还没有找到任何类似的 LINQ 示例。 rangeDays 大约在 5 到 200 之间,q1 是 MyClass 列表,其中 RowID 大约从 10000 到 25000,没有间隙。
public class MyClass { public int RowID; public object otherData; }
PopulateRange(int rangeDays, List<MyClass> q1){
var q2 = (from a in q1
let Rows = new int[rangeDays]
select new {a.RowID, Rows }).ToList();
foreach(var a in q2)
{
for(int i = 0; i < rangeDays; i++)
{
a.Rows[i] = a.RowID + i;
}
}
提前致谢。
更新: 我使用 2 个 linq 语句运行它,如下所示(希望这次全部可以运行)。
public List<MyClass> PopulateRange(int rangeDays, IQueryable<TheirClass> q1)
{
var q2 = (from a in q1
select new MyClass()
{ RowID = a.RowID, Rows = new int[rangeDays] }).ToList();
q2.ForEach(a => a.Rows = Enumerable.Range(0, rangeDays).
Select(i => i + a.RowID).ToArray());
return q2;
}
public class MyClass
{
public int RowID;
public int[] Rows;
}
public class TheirClass
{
public int RowID;
public int ID;
public string Symb;
public DateTime? EventTime;
public decimal? Value;
}
这是可以接受的,但有谁知道为什么下面的单个语句会抛出 NotSupportedException“本地序列不能在除 Contains 运算符之外的查询运算符的 LINQ to SQL 实现中使用。”当我尝试编译时跑步:
public List<MyClass> PopulateRange(int rangeDays, IQueryable<TheirClass> q1)
{
var q2 = (from a in q1
select new MyClass()
{ RowID = a.RowID, Rows = Enumerable.Range(0, rangeDays).
Select(i => i + a.RowID).ToArray() }).ToList();
return q2;
}
I can't figure out how to do the second part of this (the for/foreach) with a LINQ expressions and haven't found any similar examples with LINQ. rangeDays will be between about 5 and 200, and q1 is a list of MyClasses where RowID is about from 10000 to 25000, without gaps.
public class MyClass { public int RowID; public object otherData; }
PopulateRange(int rangeDays, List<MyClass> q1){
var q2 = (from a in q1
let Rows = new int[rangeDays]
select new {a.RowID, Rows }).ToList();
foreach(var a in q2)
{
for(int i = 0; i < rangeDays; i++)
{
a.Rows[i] = a.RowID + i;
}
}
Thanks in advance.
Update:
I got this running with 2 linq statements as shown below (hopefully this is all runnable this time).
public List<MyClass> PopulateRange(int rangeDays, IQueryable<TheirClass> q1)
{
var q2 = (from a in q1
select new MyClass()
{ RowID = a.RowID, Rows = new int[rangeDays] }).ToList();
q2.ForEach(a => a.Rows = Enumerable.Range(0, rangeDays).
Select(i => i + a.RowID).ToArray());
return q2;
}
public class MyClass
{
public int RowID;
public int[] Rows;
}
public class TheirClass
{
public int RowID;
public int ID;
public string Symb;
public DateTime? EventTime;
public decimal? Value;
}
This is acceptable, but does anyone know why the following single statement throws a NotSupportedException "Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator." when I try to compile & run:
public List<MyClass> PopulateRange(int rangeDays, IQueryable<TheirClass> q1)
{
var q2 = (from a in q1
select new MyClass()
{ RowID = a.RowID, Rows = Enumerable.Range(0, rangeDays).
Select(i => i + a.RowID).ToArray() }).ToList();
return q2;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Ani 的答案略有不同:
差异:
A slight variation on Ani's answer:
Differences:
我认为
q1
的类型实际上应该是List
或类似的类型(它当然不能是List
)。您可能想要:
I think
q1
's type should actually beList<MyRowClass>
or similar (it certainly can't be aList<int>
).You probably want: