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?

  • \$\begingroup\$ 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! :) \$\endgroup\$ Jun 26, 2014 at 16:06
  • \$\begingroup\$ it's actually both it's a copy of a reference \$\endgroup\$
    – Rune FS
    Jun 26, 2014 at 19:42
  • \$\begingroup\$ “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#. \$\endgroup\$
    – svick
    Jun 28, 2014 at 14:08

1 Answer 1


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?

  • \$\begingroup\$ 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) \$\endgroup\$
    – Donniewiko
    Jun 26, 2014 at 16:00
  • \$\begingroup\$ @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. \$\endgroup\$ Jun 26, 2014 at 16:39
  • \$\begingroup\$ @SimonAndréForsberg I edited the code to remove the bug of a possible NullReferenceException. \$\endgroup\$
    – ANeves
    Jun 26, 2014 at 16:56
  • \$\begingroup\$ @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. \$\endgroup\$ Jun 26, 2014 at 17:00
  • \$\begingroup\$ your first example will fail if either the first or the last element in the list is null and that that null value is not the only null value in the list. E.g. if both the first and the last element are null then they will both have first and last set to true regardless of how many elements there's in the list \$\endgroup\$
    – Rune FS
    Jun 26, 2014 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.