locked
HIghlight every 4th row in linq to xml query RRS feed

  • Question

  • User940894612 posted

    Hi

    I have done a query of an xml document (here is a sample) 

    Result>
    <item>
    <label>NDP-www.xxx.at</label>
    <location>New York, NY</location>
    <statusCode>UP</statusCode>
    </item>
    <item>
    <label>NDP-www.xxx.at</label>
    <location>New York, NY</location>
    <statusCode>UP</statusCode>
    </item>
    <item>
    <label>NDP-www.xxx.at</label>
    <location>New York, NY</location>
    <statusCode>UP</statusCode>
    </item>
    <item>
    <label>NDP-www.xxx.at</label>
    <location>New York, NY</location>
    <statusCode>UP</statusCode>
    </item>
    <item>
    <label>NDP-www.xxx.at</label>
    <location>New York, NY</location>
    <statusCode>UP</statusCode>
    </item>
    <item>
    <label>NDP-www.xxx.at</label>
    <location>New York, NY</location>
    <statusCode>UP</statusCode>
    </item>
    <item>
    <label>NDP-www.xxx.at</label>
    <location>New York, NY</location>
    <statusCode>UP</statusCode>
    </item>
    <item>
    <label>NDP-www.xxx.at</label>
    <location>New York, NY</location>
    <statusCode>UP</statusCode>
    </item>
    <item>
    <label>NDP-www.xxx.at</label>
    <location>New York, NY</location>
    <statusCode>UP</statusCode>
    </item>
    <item>
    <label>NDP-www.xxx.at</label>
    <location>New York, NY</location>
    <statusCode>UP</statusCode>
    </item>
    <item>
    <label>NDP-www.xxx.at</label>
    <location>New York, NY</location>
    <statusCode>UP</statusCode>
    </item>
    <item>
    <label>NDP-www.xxx.at</label>
    <location>New York, NY</location>
    <statusCode>UP</statusCode>
    </item>
    </Result>

    var query = from p in document.Elements("Result").Elements("item")
                        orderby (string) p.Element("statusCode") descending, (string) p.Element("label") ascending 
                        select new
                        {
                            Label = (string) p.Element("label"),
                            Target = (string) p.Element("location"),
                            StatusCode = (string)p.Element("statusCode")
                        };

    For every 4th item I would like to highlight the word. 

    I've tried this but cannot access the values

    for (int i = 0; i < query.Count(); i++)
                    {
                        
                        int y = i % 4;
                        if (y == 0)
                        {
                            
                        }
                        else
                        {
                            
                        }
                    }

    Any ideas how I can achieve this?

    Thanks

    Rob

    Friday, November 3, 2017 3:52 PM

All replies

  • User2103319870 posted

    'I have tried this but cannot access the values

    Any ideas how I can achieve this?

    You can try with the below code.

    Modify your existing Linq Query to add a row number for each records. You can use the Select method Overload which allow us to pass a index number

      var query = document.Elements("Result").Elements("item").AsEnumerable()
                            .OrderByDescending(k => k.Element("statusCode").Value)
                            .OrderBy(t => t.Element("label").Value)
                            .Select((y, index) => new
                            {
                                label = y.Element("label").Value,
                                location = y.Element("location").Value,
                                statusCode = y.Element("statusCode").Value,
                                RowNumber = index + 1
                            }).ToList();

    Now you can access value inside your for loop based on RowNumber

    for (int i = 1; i < query.Count()+1; i++)
                {
    
                    int y = i % 4;
                    if (y == 0)
                    {
                        //Fetch the values here
                        var result = query.AsEnumerable()
                                     .Where(h => h.RowNumber == i)
                                     .Select(u => new
                                     {
                                         label = u.label,
                                         location = u.location,
                                         statusCode = u.statusCode
                                     });
                    }
                    else
                    {
    
                    }
                }

    Friday, November 3, 2017 7:23 PM