Dlaczego nie mogę dodać elementów do listy będącej własnością klasy?

0

Mam następujący fragment kodu:

      foreach (MMS mMS in mMSes)
      {
        IEnumerable<Part> parts = from part in Messages.Elements("mms").Descendants("parts").Descendants("part")
                                  where (ulong)part.Parent.Parent.Attribute("date") == mMS.Date
                                  orderby (sbyte)part.Attribute("seq")
                                  select new Part
                                  {
                                    Text = (string)part.Attribute("text"),
                                    Data = (string)part.Attribute("data")
                                  };
        foreach (Part part in parts)
        {
          Part newPart = new Part(part);
          mMS.Parts.Add(newPart);
        }
      }

Dlaczego w wierszu:

          mMS.Parts.Add(newPart);

dodawanie elementów przebiega prawidłowo, natomiast nie jest to przenoszone do listy "mMSes", której "mMS" jest iteratorem? Wszystkie elementy listy "mMSes" mają puste listy "Parts" po wykonaniu powyższego kodu.

1

w pętli foreach pracujemy na kopiach tylko do odczytu

https://kodify.net/csharp/loop/foreach-read-only/

3
mgrzegor napisał(a):

w pętli foreach pracujemy na kopiach tylko do odczytu

Nie można modyfikować oryginalnej kolekcji, tzn. usuwać albo dodawać do niej elementów, nie można też podmienić elementu (przez przypisanie = new Cośtam()). Natomiast wartości właściwości czy pól jak najbardziej zmieniać można.

@MarcinG: skąd wiesz, że dodawanie elementów przebiega prawidłowo?

0

...

@MarcinG: skąd wiesz, że dodawanie elementów przebiega prawidłowo?

Gdy podczas debugowania "najadę" sobie wskaźnikiem myszy na obiekt "mMS" to mam w nim wypełnioną listę "Parts" interesującymi mnie obiektami. Gdy to samo zrobię na jednym z elementów listy "mMSes", to lista "Parts" któregokolwiek z elementów listy "mMSes" jest zawsze pusta, tzn. istnieje, ale zawsze ma 0 elementów. Czyli brak jest przenoszenia listy "Parts" z obiektu iteratora "mMS" do obiektu "mMSes(i)" , gdzie i to kolejny indeks, oczywiście w tej pętli nie ma indeksu, ale na podglądzie debuger pokazuje to w taki sposób.
W zwykłej pętli for jest tak samo.

1

Prawdopodobnie mMSes jest immutable i zwraca iterator na listę kopii elementów. Możesz to łatwo sprawdzić w debugerze sprawdzając

Object.ReferenceEquals(mMSes.First(), mMSes.First())
  • jeśli to zwróci false to znaczy że za każdym razem dostajesz nowy obiekt będący klonem oryginału.
    W takim przypadku musisz się odwołać do metod mutujących elementy w obiekcie mMSes bezpośrednio szukając ich oryginalnych odpowiedników po ID albo czymś podobnym, lub jeśli takich metod nie udostępnia - stworzyć całkiem nowy obiekt ze zmienionymi elementami. Jeśli potrzebujesz więcej pomocy to chociaż napisz co to za biblioteka, obiekt, a najlepiej załącz cały kod
0
obscurity napisał(a):

Prawdopodobnie mMSes jest immutable i zwraca iterator na listę kopii elementów. Możesz to łatwo sprawdzić w debugerze sprawdzając

Object.ReferenceEquals(mMSes.First(), mMSes.First())
  • jeśli to zwróci false to znaczy że za każdym razem dostajesz nowy obiekt będący klonem oryginału.
    W takim przypadku musisz się odwołać do metod mutujących elementy w obiekcie mMSes bezpośrednio szukając ich oryginalnych odpowiedników po ID albo czymś podobnym, lub jeśli takich metod nie udostępnia - stworzyć całkiem nowy obiekt ze zmienionymi elementami. Jeśli potrzebujesz więcej pomocy to chociaż napisz co to za biblioteka, obiekt, a najlepiej załącz cały kod

Ten warunek zwraca 'false'.
To jest klasa, którą wygenerowało mi VS na podstawie XML-a z tego tematu:
https://4programmers.net/Forum/C_i_.NET/343757-linq_to_xml_pobieranie_wezlow_wraz_z_potomnymi?p=1705301#id1705301
Oczywiście niecą ją już pozmieniałem, bo na początku nie dało się przypisać listy do własności Parts, więc teraz tworzę ją w konstruktorze, ale znowu nie mogę wypełnić. Co za pi, pi, pi...

  /// <remarks/>
  //[System.SerializableAttribute()]
  //[System.ComponentModel.DesignerCategoryAttribute("code")]
  //[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
  public /*partial */class Part
  {
    private byte seq;
    private string ct;
    private string name;
    private string chset;
    private string cd;
    private string fn;
    private string cl;
    private string ctt_s;
    private string ctt_t;
    private string text;
    private string data;

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Seq
    {
      get
      {
        return this.seq;
      }
      set
      {
        this.seq = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Ct
    {
      get
      {
        return this.ct;
      }
      set
      {
        this.ct = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Name
    {
      get
      {
        return this.name;
      }
      set
      {
        this.name = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Chset
    {
      get
      {
        return this.chset;
      }
      set
      {
        this.chset = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Cd
    {
      get
      {
        return this.cd;
      }
      set
      {
        this.cd = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Fn
    {
      get
      {
        return this.fn;
      }
      set
      {
        this.fn = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Cl
    {
      get
      {
        return this.cl;
      }
      set
      {
        this.cl = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Ctt_s
    {
      get
      {
        return this.ctt_s;
      }
      set
      {
        this.ctt_s = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Ctt_t
    {
      get
      {
        return this.ctt_t;
      }
      set
      {
        this.ctt_t = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Text
    {
      get
      {
        return this.text;
      }
      set
      {
        this.text = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Data
    {
      get
      {
        return this.data;
      }
      set
      {
        this.data = value;
      }
    }
  
    public Part()
    {

    } //Part

    public Part(byte seq,
                  string ct,
                  string name,
                  string chset,
                  string cd,
                  string fn,
                  string cl,
                  string ctt_s,
                  string ctt_t,
                  string text,
                  string data
                )
    {
      this.seq = seq;
      this.ct = ct;
      this.name = name;
      this.chset = chset;
      this.cd = cd;
      this.fn = fn;
      this.cl = cl;
      this.ctt_s = ctt_s;
      this.ctt_t = ctt_t;
      this.text = text;
      this.data = data;
    } //Part

    public Part(Part part)
    {
      seq = part.Seq;
      ct = part.Ct;
      name = part.Name;
      chset = part.Chset;
      cd = part.Cd;
      fn = part.Fn;
      cl = part.Cl;
      ctt_s = part.Ctt_s;
      ctt_t = part.Ctt_t;
      text = part.Text;
      data = part.Data;
    } //Part
  } //Part





  // NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
  /// <remarks/>
  //[System.SerializableAttribute()]
  //[System.ComponentModel.DesignerCategoryAttribute("code")]
  //[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
  //[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
  public /*partial */class MMS
  {
    private ulong date;
    private string snippet;
    private byte block_type;
    private string ct_t;
    private string source;
    private byte msg_box;
    private long address;
    private string sub_cs;
    private byte preview_type;
    private string mx_id;
    private string retr_st;
    private string d_tm;
    private uint exp;
    private byte locked;
    private string m_id;
    private byte out_time;
    private string retr_txt;
    private byte date_sent;
    private byte read;
    private string rpt_a;
    private string ct_cls;
    private byte timed;
    private byte pri;
    private byte sync_state;
    private string resp_txt;
    private string ct_l;
    private byte sim_id;
    private byte d_rpt;
    private byte marker;
    private string file_id;
    private byte preview_data_ts;
    private byte m_type;
    private string mx_extension;
    private byte rr;
    private byte favorite_date;
    private string sub;
    private string read_status;
    private byte date_ms_part;
    private byte seen;
    private byte bind_id;
    private string mx_id_v2;
    private byte advanced_seen;
    private byte resp_st;
    private byte text_only;
    private byte need_download;
    private string st;
    private string retr_txt_cs;
    private uint m_size;
    private byte mx_status;
    private string tr_id;
    private byte mx_type;
    private byte deleted;
    private string m_cls;
    private byte v;
    private string account;
    private string preview_data;
    private string readable_date;
    private string contact_name;
    private MMSAddr[] addrs;
    //private PartsList parts;
    //private Part[] parts;
    private List<Part> parts= new List<Part>();

    /// <remarks/>
    //[System.Xml.Serialization.XmlArrayItemAttribute("addr", IsNullable = false)]
    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public ulong Date
    {
      get
      {
        return this.date;
      }
      set
      {
        this.date = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Snippet
    {
      get
      {
        return this.snippet;
      }
      set
      {
        this.snippet = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Block_type
    {
      get
      {
        return this.block_type;
      }
      set
      {
        this.block_type = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Ct_t
    {
      get
      {
        return this.ct_t;
      }
      set
      {
        this.ct_t = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Source
    {
      get
      {
        return this.source;
      }
      set
      {
        this.source = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Msg_box
    {
      get
      {
        return this.msg_box;
      }
      set
      {
        this.msg_box = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public long Address
    {
      get
      {
        return this.address;
      }
      set
      {
        this.address = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Sub_cs
    {
      get
      {
        return this.sub_cs;
      }
      set
      {
        this.sub_cs = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Preview_type
    {
      get
      {
        return this.preview_type;
      }
      set
      {
        this.preview_type = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Mx_id
    {
      get
      {
        return this.mx_id;
      }
      set
      {
        this.mx_id = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Retr_st
    {
      get
      {
        return this.retr_st;
      }
      set
      {
        this.retr_st = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string D_tm
    {
      get
      {
        return this.d_tm;
      }
      set
      {
        this.d_tm = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public uint Exp
    {
      get
      {
        return this.exp;
      }
      set
      {
        this.exp = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Locked
    {
      get
      {
        return this.locked;
      }
      set
      {
        this.locked = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string M_id
    {
      get
      {
        return this.m_id;
      }
      set
      {
        this.m_id = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Out_time
    {
      get
      {
        return this.out_time;
      }
      set
      {
        this.out_time = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Retr_txt
    {
      get
      {
        return this.retr_txt;
      }
      set
      {
        this.retr_txt = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Date_sent
    {
      get
      {
        return this.date_sent;
      }
      set
      {
        this.date_sent = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Read
    {
      get
      {
        return this.read;
      }
      set
      {
        this.read = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Rpt_a
    {
      get
      {
        return this.rpt_a;
      }
      set
      {
        this.rpt_a = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Ct_cls
    {
      get
      {
        return this.ct_cls;
      }
      set
      {
        this.ct_cls = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Timed
    {
      get
      {
        return this.timed;
      }
      set
      {
        this.timed = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Pri
    {
      get
      {
        return this.pri;
      }
      set
      {
        this.pri = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Sync_state
    {
      get
      {
        return this.sync_state;
      }
      set
      {
        this.sync_state = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Resp_txt
    {
      get
      {
        return this.resp_txt;
      }
      set
      {
        this.resp_txt = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Ct_l
    {
      get
      {
        return this.ct_l;
      }
      set
      {
        this.ct_l = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Sim_id
    {
      get
      {
        return this.sim_id;
      }
      set
      {
        this.sim_id = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte D_rpt
    {
      get
      {
        return this.d_rpt;
      }
      set
      {
        this.d_rpt = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Marker
    {
      get
      {
        return this.marker;
      }
      set
      {
        this.marker = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string File_id
    {
      get
      {
        return this.file_id;
      }
      set
      {
        this.file_id = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Preview_data_ts
    {
      get
      {
        return this.preview_data_ts;
      }
      set
      {
        this.preview_data_ts = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte M_type
    {
      get
      {
        return this.m_type;
      }
      set
      {
        this.m_type = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Mx_extension
    {
      get
      {
        return this.mx_extension;
      }
      set
      {
        this.mx_extension = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Rr
    {
      get
      {
        return this.rr;
      }
      set
      {
        this.rr = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Favorite_date
    {
      get
      {
        return this.favorite_date;
      }
      set
      {
        this.favorite_date = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Sub
    {
      get
      {
        return this.sub;
      }
      set
      {
        this.sub = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Read_status
    {
      get
      {
        return this.read_status;
      }
      set
      {
        this.read_status = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Date_ms_part
    {
      get
      {
        return this.date_ms_part;
      }
      set
      {
        this.date_ms_part = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Seen
    {
      get
      {
        return this.seen;
      }
      set
      {
        this.seen = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Bind_id
    {
      get
      {
        return this.bind_id;
      }
      set
      {
        this.bind_id = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Mx_id_v2
    {
      get
      {
        return this.mx_id_v2;
      }
      set
      {
        this.mx_id_v2 = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Advanced_seen
    {
      get
      {
        return this.advanced_seen;
      }
      set
      {
        this.advanced_seen = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Resp_st
    {
      get
      {
        return this.resp_st;
      }
      set
      {
        this.resp_st = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Text_only
    {
      get
      {
        return this.text_only;
      }
      set
      {
        this.text_only = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Need_download
    {
      get
      {
        return this.need_download;
      }
      set
      {
        this.need_download = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string St
    {
      get
      {
        return this.st;
      }
      set
      {
        this.st = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Retr_txt_cs
    {
      get
      {
        return this.retr_txt_cs;
      }
      set
      {
        this.retr_txt_cs = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public uint M_size
    {
      get
      {
        return this.m_size;
      }
      set
      {
        this.m_size = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Mx_status
    {
      get
      {
        return this.mx_status;
      }
      set
      {
        this.mx_status = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Tr_id
    {
      get
      {
        return this.tr_id;
      }
      set
      {
        this.tr_id = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Mx_type
    {
      get
      {
        return this.mx_type;
      }
      set
      {
        this.mx_type = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Deleted
    {
      get
      {
        return this.deleted;
      }
      set
      {
        this.deleted = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string M_cls
    {
      get
      {
        return this.m_cls;
      }
      set
      {
        this.m_cls = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte V
    {
      get
      {
        return this.v;
      }
      set
      {
        this.v = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Account
    {
      get
      {
        return this.account;
      }
      set
      {
        this.account = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Preview_data
    {
      get
      {
        return this.preview_data;
      }
      set
      {
        this.preview_data = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Readable_date
    {
      get
      {
        return this.readable_date;
      }
      set
      {
        this.readable_date = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Contact_name
    {
      get
      {
        return this.contact_name;
      }
      set
      {
        this.contact_name = value;
      }
    }

    public MMSAddr[] Addrs
    {
      get
      {
        return this.addrs;
      }
      set
      {
        this.addrs = value;
      }
    }

    //public PartsList Parts
    //public Part[] Parts
    public List<Part> Parts
    {
      get
      {
        return this.parts;
      }
      set
      {
        this.parts = value;
      }
    }

    //public MMS()
    //{
    //  parts = new List<Part>();
    //}
  } //MMS

  /// <remarks/>
  //[System.SerializableAttribute()]
  //[System.ComponentModel.DesignerCategoryAttribute("code")]
  //[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
  public partial class MMSAddr
  {
    private long address;
    private byte type;
    private byte charset;

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public long Address
    {
      get
      {
        return this.address;
      }
      set
      {
        this.address = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Type
    {
      get
      {
        return this.type;
      }
      set
      {
        this.type = value;
      }
    }

    /// <remarks/>
    //[System.Xml.Serialization.XmlAttributeAttribute()]
    public byte Charset
    {
      get
      {
        return this.charset;
      }
      set
      {
        this.charset = value;
      }
    }
  } //MMSAddr

0

Musisz pokazać więcej kodu bo wygląda na to że ten kod powinien działać.
I to nie tak się pracuje z automatycznie wygenerowanymi klasami - nie powinieneś ich w ogóle ruszać, są one oznaczone jako partial celowo żebyś mógł je rozszerzać z osobnych plików i tam wprowadzać swoje zmiany. Oryginalnych plików nie powinieneś ruszać tak żeby w każdej chwili można było je wygenerować od nowa w razie zmian w strukturze XMLa.

A gdzie tworzysz listę parts? Bo ja widzę jedynie że konstruktor jest zakomentowany.
Kolejne pytanie - czym jest mMSes

0

Zacząłem zmieniać gdy mi nie działało. Oryginalnie zamiast listy wygenerowało tablicę.
Wychodzi na to, że muszę nadpisać metodę "Add" co dla mnie jest bez sensu.
To prawie cały kod. Wcześniej niż to co podałem na początku wątku jest tylko:

      IEnumerable<MMS> mMSes = from item in Messages.Elements("mms")
                                where (string) item.Attribute("address") == "+48123456789"
                                orderby (long) item.Attribute("date")
                                select new MMS
                                {
                                  Date = (ulong)item.Attribute("date"),

                                  Msg_box = Convert.ToByte((int)item.Attribute("msg_box")),
                                  Address = (long)item.Attribute("address"),

                                  Sub = (string)item.Attribute("sub"),

                                  Readable_date = (string)item.Attribute("readable_date"),
                                  Contact_name = (string)item.Attribute("contact_name")//,

                                  //Parts = new Part[]
                                };

to mi działa. Wiadomości są pobierane. Potem do każdej z nich doczytuję zawartość własności Parts jak w kodzie na samej górze. bo nie wiem jak to zrobić od razu.
Może krótkie wyjaśnienie. Jest taka aplikacja pod Androida SMS Backup&Restore. Właśnie w takim formacie zapisuje MMS-y. Każdy MMS ma 3 części ("Parts"). W pierwszej jest kod HTML, w drugiej właściwy obrazek, w trzeciej tekst, który ewentualnie dopiszemy do wiadomości. Poza tym są tam inne rzeczy, które teraz nas nie obchodzą, więc nie będę wymieniał.

0
MarcinG napisał(a):

To prawie cały kod. Wcześniej niż to co podałem na początku wątku jest tylko:

no i widzisz - tu jest cały problem. mMSes to nie żadna lista tylko zwykły enumerable i tworzy nowe obiekty MMS za każdym razem kiedy o nie pytasz. Potem te stworzone elementy zmieniasz i wyrzucasz bo nigdzie ich nie zapamiętujesz. Gdy drugi raz odpytasz mMSes stworzone zostaną całkowicie nowe obiekty. Musisz zmaterializować tę listę dodając na przykład .ToList().
Gdybyś miał resharpera to ostrzegł by Cię przed tym warningiem:

Possible multiple enumeration of IEnumerable

0

Dzięki, to było to.

1 użytkowników online, w tym zalogowanych: 0, gości: 1