LINQ 填充范围

发布于 2024-10-16 16:05:21 字数 1636 浏览 7 评论 0原文

我不知道如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枯寂 2024-10-23 16:05:21

Ani 的答案略有不同:

var q2 = q1.Select(a => new { Rows = Enumerable.Range(a.RowID, rangeDays)
                                               .ToArray(),
                              RowID = a.RowID });

差异:

  • 当只有一个选择时,我不关心查询表达式语法
  • 而不是使用从 0 开始的范围然后选择,我认为从 a.RowID 开始会更容易: )

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 :)
多情出卖 2024-10-23 16:05:21

我认为 q1 的类型实际上应该是 List 或类似的类型(它当然不能是 List)。
您可能想要:

var q2 = (from a in q1
          select new
          {
              a.RowID,
              Rows = Enumerable.Range(0, rangeDays)
                               .Select(i => i + a.RowID)
                               .ToArray()

          }).ToList();

I think q1's type should actually be List<MyRowClass> or similar (it certainly can't be a List<int>).
You probably want:

var q2 = (from a in q1
          select new
          {
              a.RowID,
              Rows = Enumerable.Range(0, rangeDays)
                               .Select(i => i + a.RowID)
                               .ToArray()

          }).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文