locked
HOW TO SELECT only 20 words from a column using LINQ RRS feed

  • Question

  • User2104364237 posted

    I guys,

    I've got a column which can contain more than 20 words but I would like to select only 10 or 20 words to be  displayed on the  web interface.

    Can  someone  help me?

    Friday, September 14, 2018 10:27 AM

All replies

  • User-821857111 posted

    Use Take:

    var sample = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
    reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
    in culpa qui officia deserunt mollit anim id est laborum."; var intro = sample.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries).Take(10); var output = string.Join(" ", intro);

    Friday, September 14, 2018 11:46 AM
  • User632428103 posted

    Hello,

    try this perhpas : 

    string[] tabs = { "a aa aaa aaaa", "b bb bbb bbbb", "c cc", "d", "e ee eee" };
    var result = (from x in tabs where x.Split(' ').Count() > 2 select x);

    result contains = a aa aaa aaaa", b bb bbb bbbb,  e ee eee

    Or if you want you can limit the string selected with a substring.

    Friday, September 14, 2018 12:05 PM
  • User-271186128 posted

    Hi dany,

    I've got a column which can contain more than 20 words but I would like to select only 10 or 20 words to be  displayed on the  web interface.

    Can  someone  help me?

    Agree with Mike, you could use the Take method.

    From your description, I guess you want to display the result on the web interface using tables, and some of the columns contain very long text, so that you want to display part of them. If that is the case, I suggest you could use ellipsis render.  Please refer to the following code:

    <style>
        table {
            font: 18px Arial;
            width: 40%;
            margin: 1em auto;
            color: #333;
            border: 1px solid rgba(153, 153, 153, 0.4);
        }
    
            table td, table th {
                text-align: left;
                padding: 1.2em 20px;
                white-space: nowrap;
                border-left: 1px solid rgba(153, 153, 153, 0.4);
            }
                table td:first-child, table th:first-child {
                    border-left: 0;
                }
            table th {
                border-bottom: 1px solid rgba(153, 153, 153, 0.4);
                font-weight: 400;
                text-transform: uppercase;
                max-width: 10vw;
            }
                table th > .wrap {
                    text-overflow: ellipsis;
                    overflow: hidden;
                    white-space: nowrap;
                }
    </style>
    <table>
        <thead>
            <tr>
                <th>
                    <div class="wrap" title="Some long title">Some long title</div>
                </th>
                <th>
                    <div class="wrap">Short</div>
                </th>
                <th>
                    <div class="wrap">medium one</div>
                </th>
                <th>
                    <div class="wrap" title="endlessly super long title which no developer likes to see">endlessly super long title which no developer likes to see</div>
                </th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>cell</td>
                <td>cell</td>
                <td>cell</td>
                <td>very long text here</td>
            </tr>
        </tbody>
    </table>

    the output as below:

    If you are using JQuery DataTable plugin, you could refer to this link: Ellipsis renderer

    Best regards,
    Dillion

    Monday, September 17, 2018 9:28 AM