简体   繁体   English

使用Linq select和String.Join插入行号

[英]Insert Row Number using Linq select with String.Join

Hi all and thanks in advance, 大家好,谢谢,

I have a question about how (or if) the following should be done. 我对如何(或是否)执行以下操作有疑问。 I have a list of items being returned and for certain pieces I am using a string.join to compile those parts into a single string on the page but now I need to add line numbers to this display. 我有要返回的项目列表,对于某些部件,我正在使用string.join将这些部件编译为页面上的单个字符串,但是现在我需要在此显示中添加行号。

Currently this is how the data is displaying and how I want it to look after numbers are added: 当前,这是数据的显示方式以及添加数字后的显示方式:

 Title (before)             Title (after)
  ---------                 ---------
  Book1                     1. Book1
  Book2                     2. Book2

The data is being displayed in a List View in a single Eval statement for each item (Title,Program) like so: 数据将在每个项目(标题,程序)的单个Eval语句的列表视图中显示,如下所示:

 <asp:ListView runat="server" ID="lvResults" AutoGenerateColumns="False" ItemPlaceholderID="resultsPlaceHolder">
      <EmptyDataTemplate>
           <div style="width: 100%; text-align: center;">No results available.</div>
      </EmptyDataTemplate>
      <LayoutTemplate>
           <table id="tblResults" summary="">
               <tbody>
                    <asp:PlaceHolder runat="server" ID="resultsPlaceHolder"></asp:PlaceHolder>
               </tbody>
           </table>
      </LayoutTemplate>
      <ItemTemplate>
           <tr>
               <td>
                    Text: <%# Eval("Text") %><br/>
                    Description: <%# Eval("Description") %><br/>
                    <br class="clear"/>
               </td>
               <td>
                    Title<br/><%# Eval("Title") %>
               </td>
               <td>
                    Program<br/><%# Eval("Value") %>
               </td>
            </tr>
      </ItemTemplate>
   </asp:ListView>

I'm populating the List View with this: 我用这个填充列表视图:

 List<MyBooks> newBooklist = oldBookList.Select(i => new MyBooks() { Id = i, Text = oldBookList.Where(o => o.Id == i).Select(o => o.Text).FirstOrDefault(), Description = oldBookList.Where(o => o.Id == i).Select(o => o.Description).FirstOrDefault(), //tried getting index here but output is wrong "{ index = 0, title = .. }" Title = string.Join("<br/>", oldBookList.Where(o => o.Id == i).Select((o,index) => new { index, o.Title }).Distinct()), Value = string.Join("<br/>", oldBookList.Where(o => o.Id == i).Select(o => o.Value).Distinct()), }).ToList(); 

I'd like to fix the above so that I can add in the numbers into the text as part of the string.join operation. 我想修复上述问题,以便将数字作为string.join操作的一部分添加到文本中。 To that end, I tried following suggestions here How To Project a Line Number Into Linq Query Result and here How to get index using LINQ? 为此,我尝试了以下建议: 如何将行号投影到Linq查询结果中,以及如何使用LINQ获取索引? [duplicate] but both examples seem to deal with have a separate property to dump the value into which I don't have in my scenario. [重复],但似乎两个示例都处理了一个单独的属性,以将我的情况下没有的值转储到其中。

Another idea that I found was to consider looping through the list Titles and adding the text before I pull this list that populates the List View but I'm concerned that would be an expensive operation, would it be? 我发现的另一个想法是,在拉动填充列表视图的列表之前,考虑遍历列表标题并添加文本,但是我担心这样做会很昂贵,是吗?

The one thing I would really prefer not to do is to add these numbers to the SQL query because this query is used in other places. 我真正不希望做的一件事是将这些数字添加到SQL查询中,因为此查询在其他地方使用。

[UPDATE] [UPDATE]

As asked in the comment below, I wanted to have numbers but I needed the numbers to further subgroup by Title and Program. 如下面评论中的要求,我想有数字,但我需要数字以标题和程序进一步细分。 The best way to achieve this finally turned out to be just iterating through the list before hand with a foreach statement to set the numbers. 最终实现这一目标的最佳方法是在遍历列表之前先使用foreach语句设置数字。

  int dt = 0; int oldId = 0; foreach (var d in oldList) { if (oldId != d.Id) { dt = 0; oldId = d.Id; } dt++; d.Title = dt + ". " + d.Title; d.Program = dt + ". " + d.Program; } 

Using enumerable.Range should get you what you want. 使用enumerable.Range应该可以为您提供所需的东西。

List<MyBooks> newBooklist = from i in Enumerable.Range(0, oldBookList.Count)
{
    Id = i, 
    Text = oldBookList[i].Text,
    Description = oldBookList[i].Description, 
    ....

}).ToList();

You use the select overload in your OP, so I assumed that you knew about it. 您在OP中使用了select重载,所以我假设您已经知道。 But in that format it would look like this: 但是以这种格式,它看起来像这样:

List<MyBooks> newBooklist = oldBookList.Select((book, i) => new MyBooks()
{
    Id = i, 
    Text = book.Text,
    Description = book.Description, 
    ....

}).ToList();

I think Jon Skeet might have the answer for you Can I have an incrementing count variable in LINQ? 我认为Jon Skeet可能为您找到了答案。我可以在LINQ中使用递增计数变量吗?

Basically there is an overload of Select that provides an index, and you can use that index in your string.Join calls. 基本上, Select的重载提供了索引,您可以在字符串中使用该索引。

stuff.Select((value, index) => new { index, value.Name });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM