I am not certain if you really need dynamic linq to do what you want. Just regular System.Linq should handle finding indexes of position and also the element at should work just fine if you are using C# .NET 4.5 or 4.0 in my experience. Here is a simple console app that should work fine:
using System;
using System.Collections.Generic;
using System.Linq;
public class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
class Program
{
public static List<User> Users;
static void Main(string[] args)
{
Users = new List<User>
{
new User
{
FirstName = "Brett",
LastName = "M"
},
new User
{
FirstName = "John",
LastName = "Doe"
},
new User
{
FirstName = "Sam",
LastName = "Dilat"
}
};
Console.WriteLine("Should work as long as object is defined and instantiated");
Console.WriteLine(Users.ElementAt(1).FirstName);
Console.WriteLine("\nIterate Through List with Linq\n I use a string you can add to another list or whatever\n\n");
string s = "";
Users.ForEach(n => s += "Position: " + Users.IndexOf(n) + " " + n.LastName + Environment.NewLine);
Console.WriteLine(s);
Console.ReadLine();
}
}
And if you just want to find a particular Object in a collection by a property of that collection you can use the 'FindIndex' method:
Console.WriteLine("Index of Dilat: " + Users.FindIndex(n => n.LastName.Equals("Dilat")));
EDIT 9-27-13 for string to split.
If you want to take a string and split it up into a nice enurable list to iterate through you can do that too. You merely need to find the seperator. If you do not have one you are going to have to use some regex magic which is more complicated.
Keep everything the same but substituting my MAIN method:
string split = "Brett,Same,John,Frank";
List<string> stringList = split.Split(',').ToList();
string s = "";
stringList.ForEach(n => s += n + Environment.NewLine);
s += "\n\nFor Observing Element at\n";
s += "John is at: " + stringList.FindIndex(n => n.Equals("John"));
s += "\n\nGetting Poco Objects assigned and listed\n";
var list = stringList.Select(u => new User
{
FirstName = u,
LastName = "Something"
}).ToList();
list.ForEach(n => s += "User: " + n.FirstName + " " + n.LastName + "At: " + list.IndexOf(n) + Environment.NewLine);