LINQ 中的行号

发布于 2024-11-07 18:47:34 字数 426 浏览 0 评论 0 原文

我有一个像这样的 linq 查询:

var accounts =
    from account in context.Accounts
    from guranteer in account.Gurantors
     where guranteer.GuarantorRegistryId == guranteerRegistryId
    select new AccountsReport
    {
        recordIndex = ?   
        CreditRegistryId = account.CreditRegistryId,
        AccountNumber = account.AccountNo,
    }

我想用 LINQ 返回的集合中当前行号的值填充 recordIndex。我怎样才能得到行号?

I have a linq query like this:

var accounts =
    from account in context.Accounts
    from guranteer in account.Gurantors
     where guranteer.GuarantorRegistryId == guranteerRegistryId
    select new AccountsReport
    {
        recordIndex = ?   
        CreditRegistryId = account.CreditRegistryId,
        AccountNumber = account.AccountNo,
    }

I want to populate recordIndex with the value of current row number in collection returned by the LINQ. How can I get row number ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

后知后觉 2024-11-14 18:47:34

linq-to-entities 不支持行号。您必须首先从数据库中检索没有行号的记录,然后通过 linq-to-objects 添加行号。像这样的东西:

var accounts =
    (from account in context.Accounts
     from guranteer in account.Gurantors
         where guranteer.GuarantorRegistryId == guranteerRegistryId
     select new
         {  
             CreditRegistryId = account.CreditRegistryId,
             AccountNumber = account.AccountNo,
         })
    .AsEnumerable()  // Moving to linq-to-objects 
    .Select((r, i) => new AccountReport
         {
             RecordIndex = i,  
             CreditRegistryId = r.CreditRegistryId,
             AccountNumber = r.AccountNo,
         });

Row number is not supported in linq-to-entities. You must first retrieve records from database without row number and then add row number by linq-to-objects. Something like:

var accounts =
    (from account in context.Accounts
     from guranteer in account.Gurantors
         where guranteer.GuarantorRegistryId == guranteerRegistryId
     select new
         {  
             CreditRegistryId = account.CreditRegistryId,
             AccountNumber = account.AccountNo,
         })
    .AsEnumerable()  // Moving to linq-to-objects 
    .Select((r, i) => new AccountReport
         {
             RecordIndex = i,  
             CreditRegistryId = r.CreditRegistryId,
             AccountNumber = r.AccountNo,
         });
浊酒尽余欢 2024-11-14 18:47:34

LINQ to object 具有适用于任何枚举器的内置功能:

http://weblogs.asp.net/fmarguerie/archive/2008/11/10/using-the-select-linq-query-operator-with-indexes.aspx

编辑:虽然 IQueryable 也支持它(此处此处)有人提到,这不幸的是不适用于 LINQ to SQL/Entities。

new []{"aap", "noot", "mies"}
    .Select( (element, index) => new { element, index });                                  

将导致:

{ { element = aap, index = 0 }, 
  { element = noot, index = 1 }, 
  { element = mies, index = 2 } }

还有其他 LINQ 扩展方法(例如 < code>.Where) 带有额外的索引参数重载

LINQ to objects has this builtin for any enumerator:

http://weblogs.asp.net/fmarguerie/archive/2008/11/10/using-the-select-linq-query-operator-with-indexes.aspx

Edit: Although IQueryable supports it too (here and here) it has been mentioned that this does unfortunately not work for LINQ to SQL/Entities.

new []{"aap", "noot", "mies"}
    .Select( (element, index) => new { element, index });                                  

Will result in:

{ { element = aap, index = 0 }, 
  { element = noot, index = 1 }, 
  { element = mies, index = 2 } }

There are other LINQ Extension methods (like .Where) with the extra index parameter overload

蓝颜夕 2024-11-14 18:47:34

尝试像这样使用 let:

int[] ints = new[] { 1, 2, 3, 4, 5 };
int counter = 0;
var result = from i in ints
             where i % 2 == 0
             let number = ++counter
             select new { I = i, Number = number };

foreach (var r in result)
{
    Console.WriteLine(r.Number + ": " + r.I);
}

我现在无法使用实际的 LINQ to SQL 或实体框架来测试它。请注意,上面的代码将在多次执行查询之间保留计数器的值。

如果您的特定提供商不支持此功能,您始终可以 foreach (从而强制执行查询)并在代码中手动分配号码。

Try using let like this:

int[] ints = new[] { 1, 2, 3, 4, 5 };
int counter = 0;
var result = from i in ints
             where i % 2 == 0
             let number = ++counter
             select new { I = i, Number = number };

foreach (var r in result)
{
    Console.WriteLine(r.Number + ": " + r.I);
}

I cannot test it with actual LINQ to SQL or Entity Framework right now. Note that the above code will retain the value of the counter between multiple executions of the query.

If this is not supported with your specific provider you can always foreach (thus forcing the execution of the query) and assign the number manually in code.

触ぅ动初心 2024-11-14 18:47:34

因为问题中的查询按单个 id 进行过滤,所以我认为给出的答案不会有帮助。当然,您可以在内存客户端完成这一切,但是根据数据集的大小以及是否涉及网络,这可能是一个问题。

如果您需要 SQL ROW_NUMBER [..] OVER [..] 等效项,我知道的唯一方法是在 SQL Server 中创建一个视图并对其进行查询。

Because the query inside the question filters by a single id, I think the answers given wont help out. Ofcourse you can do it all in memory client side, but depending how large the dataset is, and whether network is involved, this could be an issue.

If you need a SQL ROW_NUMBER [..] OVER [..] equivalent, the only way I know is to create a view in your SQL server and query against that.

山人契 2024-11-14 18:47:34

经过测试并且有效:

按如下方式修改您的代码:

int counter = 0; 
var accounts =
    from account in context.Accounts
    from guranteer in account.Gurantors
     where guranteer.GuarantorRegistryId == guranteerRegistryId
    select new AccountsReport
    {
        recordIndex = counter++   
        CreditRegistryId = account.CreditRegistryId,
        AccountNumber = account.AccountNo,
    }

希望这有帮助..虽然已经晚了:)

This Tested and Works:

Amend your code as follows:

int counter = 0; 
var accounts =
    from account in context.Accounts
    from guranteer in account.Gurantors
     where guranteer.GuarantorRegistryId == guranteerRegistryId
    select new AccountsReport
    {
        recordIndex = counter++   
        CreditRegistryId = account.CreditRegistryId,
        AccountNumber = account.AccountNo,
    }

Hope this helps.. Though its late:)

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