locked
Getting rid of rows without values in WebGrid RRS feed

  • Question

  • User-1595131707 posted

    I have the following code to get values in out of a comma separated Tags. When the tagname is the same as a parameter (in my case name), I want it to show the title & tag. But, if there is no similarity, I don't want the rows to be shown. Any help would be appreciated. Thanks!

            @grid.GetHtml( 
                    tableStyle: "table",
                    columns: grid.Columns(
                    grid.Column("Title", "Title", @<text>@foreach(var m in item.Tags.ToString().Split(new [] {','})) { if(m == name) {@item.Title <br /> @m }else { } } </text>),
                    grid.Column("CreateOn", "Date", @<text> @item.CreateOn.ToString("MM.dd.yyyy")</text>)
                    )
        ) 

    Wednesday, March 11, 2015 11:16 PM

Answers

  • User-821857111 posted

    You may need something like this:

    var data = db.Query(yourSql);
    data = data.Where(d => d.Tags.ToString().Split(new [] {','}).Any(t => t == name));
    var grid = new WebGrid(data);

    However, if the "name" variable is unlikely to include commas, and Tags is just a string with commas, you could probably get away with this:

    var data = db.Query(yourSql);
    data = data.Where(d => d.Tags.Contains(name));
    var grid = new WebGrid(data);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 12, 2015 11:46 AM

All replies

  • User-821857111 posted
    You need to filter out the unwanted rows before you bind the day to the webgrid.
    Thursday, March 12, 2015 1:56 AM
  • User-1595131707 posted

    Thanks Mike. But how so? Can you suggest?

    Thursday, March 12, 2015 9:31 AM
  • User-821857111 posted

    You may need something like this:

    var data = db.Query(yourSql);
    data = data.Where(d => d.Tags.ToString().Split(new [] {','}).Any(t => t == name));
    var grid = new WebGrid(data);

    However, if the "name" variable is unlikely to include commas, and Tags is just a string with commas, you could probably get away with this:

    var data = db.Query(yourSql);
    data = data.Where(d => d.Tags.Contains(name));
    var grid = new WebGrid(data);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 12, 2015 11:46 AM
  • User-1595131707 posted

    Thanks a ton!

    Monday, March 16, 2015 11:26 AM