How can I move next/previous in List using LINQ?

I have this class :

public class City
{
    public City()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public int Id { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public string Image { get; set; }
}

and this is my list :

List<City> list = new List<City>(){
new City{Id=1,Title="city1",Description=""},
new City{Id=2,Title="city2",Description=""},
new City{Id=3,Title="city3",Description=""},
new City{Id=4,Title="city4",Description=""}

}

How can I move next or move previous in my List using LINQ ?

Answers


You want ElementAt, it's logically equivalent to using the list indexer property, and is potentially far faster than using Skip & Take at runtime as it contains a specific check to see if the sequence implements IList, if so it uses the indexer, if not it iterates over the sequence (similar to doing a Skip(n).Take(1))

var nameList = List<string>{"Homer", "Marge", "Bart", "Lisa", "Maggie"};
IEnumerable<string> nameSequence = nameList;

secondFromList = nameList[1];
secondFromSequence = nameSequence.ElemenetAt(1);

Hope this is clear enough.

NB: IList is implemented by all sorts of collections, arrays etc not just List<>


Need Your Help

Php live google chart

php mysql

i'm working on a chart from mysql and i want to do it live or to update itself every 10 sec.

Linking commits to issues - best practices

version-control mercurial project-management dvcs redmine

We are trying to introduce what we hope is a good development practice: every commit must be linked to an issue in issue tracking system. (It's perfectly acceptable to create a new issue in order to

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.