1

I have a function. This function takes an IEnumerable<Customer> (Customer being an entity). What the function needs to do is tell the DataContext (which has a collection of Customers as a property) that its Customers property needs to be overwritten with this passed in IEnumerable<Customer>. I can't use assignment because DomainContext.Customers cannot be assigned to, as it is read only.

I guess it's not clear what I'm asking, so I suppose I should say... how do I do that? So we have DataContext.Customers (of type System.Data.Linq.Table) which wants to be replaced with a System.Collections.Generic.IEnumerable. I can't just assign the latter to the former because DataContext's properties are read only. But there must be a way.

Edit: here's an image:

enter image description here

Further edit: Yes, this image does not feature a collection of the type 'Customer' but rather 'Connection'. It doesn't matter though, they are both created from tables within the linked SQL database. So there is a dc.Connections, a dc.Customers, a dc.Media and so on.

Thanks in advance.

Kit
  • 20,354
  • 4
  • 60
  • 103
user738383
  • 597
  • 2
  • 8
  • 18
  • 1
    I'm not sure what you're asking or how this relates to WCF... – Tad Donaghe Dec 05 '11 at 15:24
  • possible duplicate of [The DataContext object and assigning its properties](http://stackoverflow.com/questions/8359199/the-datacontext-object-and-assigning-its-properties) You should update that question if you have not yet found your answer. – Andrew Barber Dec 05 '11 at 15:26
  • Sorry Terry, I thought the DataContext object fell under WCF. What does it fall under if not that? – user738383 Dec 05 '11 at 15:40

4 Answers4

0

Your collection IsReadonly. This can happen when you have an Interface in you viewstate, or an other place which serializes the collection. If it is deserialized it is readonly because it does not have a implementation of the interface its functions.

Peter
  • 27,590
  • 8
  • 64
  • 84
0

I assume you want to UPDATE all Customer entities in the context having same ID of the entities in your IEnumerable, you can use an approach like in this answer:

https://stackoverflow.com/a/4212581/559144

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
0

Is it not DataContext.SubmitChanges()??

Answer version (for those struggling to see that this rhetorical question was a proposed answer):

It's DataContext.SubmitChanges()

  • Before I can submit the changes, I need to make sure the DataContext has the changes, which is what I'm doing. As for why I'm doing it this way, it's a not particularly interesting story. – user738383 Dec 05 '11 at 15:44
  • Haha I'm sure it isn't. Have you got a bit of code we can look at? I don't know a great deal mate but might be able to help if you give us an example. –  Dec 05 '11 at 16:00
  • Okay, I added an image to the main question which contains code. – user738383 Dec 05 '11 at 17:11
  • It does provide an answer. It was just worded in a way that maybe made it look more appropriate as a comment - everybody else who read it seemed to see past that though. Edited –  Nov 19 '12 at 08:02
0

It's normal, Collections should be readonly most of the time.

You just have to clear the collection, then add every new item using a foreach:

private void SetItems(IEnumerable<MyObject> _NewItems)
{
    myCollection.Clear();
    foreach (MyObject newItem in _NewItems)
    {
        myCollection.Add(newItem);
    }
}
Louis Kottmann
  • 16,268
  • 4
  • 64
  • 88
  • This does look like what I'm looking for, except the type in the collections is System.Data.Linq.Table and there are no 'clear' or 'remove' options so far as I can see. – user738383 Dec 05 '11 at 15:47
  • You can use myTable.RemoveAll(myTable) in your case i believe. – Louis Kottmann Dec 05 '11 at 16:04
  • I altered the main question and added an image. As you can see, there is nothing under 'R' except 'IsReadOnly' and 'Reverse<>'. – user738383 Dec 05 '11 at 17:12
  • What is the type of dc.Connections? there is no "Connections" property in System.Data.Linq.Table, see http://msdn.microsoft.com/en-us/library/bb358844.aspx – Louis Kottmann Dec 05 '11 at 17:16
  • I beg your pardon, in the example I use the entity 'Collections' rather than 'Customers'. It's just a table that has been imported from the database. Other tables include 'Customers' and 'Users'. – user738383 Dec 05 '11 at 18:09