问题描述
我不知道如何用LINQ表达式来完成这个(for / foreach)的第二部分,还没有找到任何与LINQ相似的例子。 rangeDays将在5到200之间,q1是MyClasses的列表,其中RowID大约在10000到25000之间,没有间隙。
public class MyClass {public int RowID;公共对象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;
code $
$ b b
更新:
我用2个linq语句来运行,如下所示(希望这次所有的都可以运行)。
public List< MyClass>我们可以通过下面的例子来说明如何使用它们来创建一个新的MyClass():$ {
$ b $ select {new MyClass()
{RowID = a.RowID,Rows = new int [rangeDays]})。ToList();
q2.ForEach(a => a.Rows = Enumerable.Range(0,rangeDays)。
选择(i => i + a.RowID).ToArray());
返回q2;
}
public class MyClass
{
public int RowID;
public int []行;
}
public class TheirClass
{
public int RowID;
public int ID;
公共字符串Symb;
public DateTime? EVENTTIME;
公开小数?值;
$ b 这是可以接受的,但没有人知道为什么下面的单个语句抛出一个NotSupportedException除了Contains操作符之外,本地序列不能用于查询操作符的LINQ to SQL实现。当我尝试编译&运行:
public List< MyClass>我们可以通过下面的例子来说明如何使用它们来创建一个新的MyClass():$ {
$ b $ select {new MyClass()
{RowID = a.RowID,Rows = Enumerable.Range(0,rangeDays)。
Select(i => i + a.RowID).ToArray()})。ToList();
返回q2;
解决方案 :
var q2 = q1.Select(a => new {Rows = Enumerable.Range(a.RowID,rangeDays)
.ToArray(),
RowID = a.RowID});
区别:
- 当只有一个选择时,我不打扰查询表达式的语法
- 而不是使用范围从0,然后选择,我认为这将是更容易开始在a.RowID:)
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;
}
解决方案 A slight variation on Ani's answer:
var q2 = q1.Select(a => new { Rows = Enumerable.Range(a.RowID, rangeDays)
.ToArray(),
RowID = a.RowID });
Differences:
- When there's just a single select, I don't bother with query expression syntax
- Rather than using Range from 0 and then Select, I figured it would be easier to just start off at a.RowID :)
这篇关于LINQ来填充范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!