LINQ - Assign a Value to Anonymous Type's Read Only Property

anonymous-types, asp.net, c#, linq

Solution

Instead of trying to change the value after creating the list, just set the right value while creating the list.

var list = from c in db.Mesai
               join s in db.MesaiTip on c.mesaiTipID equals s.ID
               where c.iseAlimID == iseAlimID
               select new
               {
                   tarih = c.mesaiTarih,
                   mesaiTip = s.ad,
                   mesaiBaslangic = c.mesaiBaslangic,
                   mesaiBitis = c.mesaiBitis,
                   sure = c.sure,
                   condition = c.onaylandiMi,
                   status = c.onaylandiMi != null ? c.status : "Not Confirmed"
               };

Also, if you could change the property, your problem would be executing the query twice: first in the `foreach`-loop, and then again by calling `list.ToList()` (which would create new instances of the anonymous type).

Problem

I would like to create an anonymous type from linq. Then change the value of a single property(status) manually and give the list to a repeater as data source. But doesn't let me do that as theay are read-only. Any suggestion? ``` var list = from c in db.Mesai join s in db.MesaiTip on c.mesaiTipID equals s.ID where c.iseAlimID == iseAlimID select new { tarih = c.mesaiTarih, mesaiTip = s.ad, mesaiBaslangic = c.mesaiBaslangic, mesaiBitis = c.mesaiBitis, sure = c.sure, condition = c.onaylandiMi, status = c.status }; foreach (var item in list) { if (item.condition==null) { item.status == "Not Confirmed"; } } rpCalisanMesai.DataSource = list.ToList(); rpCalisanMesai.DataBind(); ```

Original source