I am trying to do the following. I have a list of Custom objects:
public class TestObject {
public int Id { get; set; }
public string Name { get; set; }
public bool First { get; set; }
public bool Last { get; set; }
}
I want to set the property First and Last to true at the first and last item in the list.
Side note: I am filling the list in a LinqToEntity
query. Perhaps its even possible in LinqToEntity
.
I have come up with the following but i doubt its the most efficient way.
var result = new List<TestObject> {
new TestObject { Id = 1, Name = "Test1" },
new TestObject { Id = 2, Name = "Test2" },
new TestObject { Id = 2, Name = "Test2" }
};
foreach (var item in result)
{
item.First = result.FirstOrDefault() == item;
item.Last = result.LastOrDefault() == item;
}
I eventually want to display the results in a ASP.Net's ListView (webforms), where I have buttons that will be displayed based on the property.
Is there an easier or more efficient way of doing this?