# Update first and last item in List<T>

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?

-
Welcome to CR, and congratulations, you now have enough reputation score to upvote! Feel free to spend as many as you like, we need more voters! :) –  Mat's Mug Jun 26 '14 at 16:01
If doing so wouldn't invalidate existing answers, it would probably be best to edit your question so as to include your actual, real code; you'd be surprised with what can come out of a peer review! :) –  Mat's Mug Jun 26 '14 at 16:06
it's actually both it's a copy of a reference –  Rune FS Jun 26 '14 at 19:42
“I never expected that when you set the value of that item, that it would set the value to the item in the list.” I think that means you should read up on how reference and value types work in C#. –  svick Jun 28 '14 at 14:08

The performance of your current code depends a little about the implementation of the list, but what I primarily would have done is to extract first and last to variables that get initialized once, not in each iteration.

var first = result.FirstOrDefault();
var last = result.LastOrDefault();
foreach (var item in result)
{
item.First = first == item;
item.Last = last == item;
}


Secondly, if you're sure that all initial items have false at both the First and Last properties, then you don't need to use a loop at all, just do something like this:

if (result.Any())
{
result.First().First = true;
result.Last().Last = true;
}


I am no C# expert but this should give you an idea.

However, I'd question why you have the First and Last properties in the first place. Are you really really sure that you need them as properties? It feels to me that there's a risk that they will be inconsistent with their actual position in the list. And if you have access to the list itself where you want to get the value of the First/Last properties, well then you don't need the properties at all, do you?

-
Thanks for your reply. The result is already a .ToList() so the FirstOrDefault and LastOrDefault should be on the List that sits in memory. And about your second reply, will that work on when there is one item in the list? I need the property to be true for both properties then. (it obviously throws an error when list is null) –  Donniewiko Jun 26 '14 at 16:00
@Donniewiko result.FirstOrDefault().First = true; result.LastOrDefault().Last = true; will work fine when there's only one item in the list, yes. Although based on your edit, I think you can do it differently (cleaner) without those properties on each item. –  Simon André Forsberg Jun 26 '14 at 16:39
@SimonAndréForsberg I edited the code to remove the bug of a possible NullReferenceException. –  ANeves Jun 26 '14 at 16:56
@Donniewiko You don't need to separate the two ifs in your solution. If a list has a first item, it also has a last item. Thanks for the edit, @ANeves. –  Simon André Forsberg Jun 26 '14 at 17:00
@SimonAndréForsberg and ANeves thanks for the help –  Donniewiko Jun 26 '14 at 18:37