Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am working on a SharePoint webpart which requires retrieval of data using LINQ. I needed the result to be on a List-type object so that I can use the result within, freely.

But the issue is that, for the declared C# List-type with an custom object casted on it; For every new item added into the list, the object that were added earlier were changed to the latest value of the object that just entered the list.

Here is a sample code of what I wrote:

List<ItemEntity> LocalItemList = new List<ItemEntity>();

var linqQuery = from item in LinqList;
                where item.Expire > DateTime.Now
                select new
                {
                    item.Title,
                    item.Message
                }

foreach (var retrievedItem in linqQuery){
    //this is an entity class object, "tempItemEntity".
    ItemEntity tempItemEntity = new ItemEntity();

    tempItemEntity.Author = retrievedItem.Title;
    tempItemEntity.Message = retrievedItem.Message;

    LocalItemList.Add(tempItemEntity);
}

/* From here on is my test printing loop to see the returned result. The results are shown below. */

Here is the result printed from the List, "LocalItemList":

Expected Result

These are the result that I am expecting.

LocalItemList[0] = "Title1" + "Message1"
LocalItemList[1] = "Title2" + "Message2"
LocalItemList[2] = "Title3" + "Message3"

Returned Result

Instead, these were the results returned.

LocalItemList[0] = "Title3" + "Message3"
LocalItemList[1] = "Title3" + "Message3"
LocalItemList[2] = "Title3" + "Message3"

It seemed to me that for every new item added to the list, the previous index gets updated and changed into the latest added item. Why is this so?

If you are talking about the object referencing is not correct, do correct me.

p.s. Apparently, if the List is not casted in the following manner, List<Object>, and instead this way, List<String>, the results are correct.


Update

I was searching around the forum and found the following questions who had similar issue with me:

  1. Duplicate List<T>.Add(T item) Items
  2. Select method in List<t> Collection

But I have problem implementing the probable solution from the answer given in Link #1.


Thank you for your time!

share|improve this question
    
Does your linqQuery variable contain the correct values? –  keyboardP Jul 3 '13 at 7:48
    
@keyboardP Yes, it contains the correct values in linqQuery. I have tested it and the linqQuery returns the correct result; but i will like to have the information to be inserted into a (C#) list.. –  Kodeist Jul 3 '13 at 9:33
    
The bug might be in your test printing loop. –  shamp00 Jul 4 '13 at 9:45
    
@shamp00: not really. it works fine actually when the <cast type> is of other types like List<String>... or List<Int>... which are okay! But not List<Object>... –  Kodeist Jul 4 '13 at 10:13
    
Have you tried a ToList() in linqQuery? –  Luis Jul 16 '13 at 16:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.