Cannot add items to an IList/List being casted from an IEnumerable
By : user2976565
Date : March 29 2020, 07:55 AM
it should still fix some issue In comments on one of the answers above you say "I'm trying to avoid copying the list." You are trying to add data to an object whose underlying type is IEnumerable . But an IEnumerable object is not an actual container, it's an interface. You can't add stuff to an interface. But you can assign another object that implements that interface to it. code :
obj2.Stuff = Enumerable.Range(0, 5000).Select(i => "Iteration " + i.ToString());
|
XmlSerializer - IEnumerable or IList as LIst
By : merbng
Date : March 29 2020, 07:55 AM
it fixes the issue Since you can't change the class, create a new class, inheriting from the class you want to serialize and from IXmlSerializable. In addition, you can override the base Colors array with the new keyword. Try this one: code :
public class Something
{
public int Id { get; set; }
public string Text { get; set; }
public IEnumerable<string> Colors { get; set; }
}
public class MySerializableSomething : Something, IXmlSerializable
{
public new List<string> Colors { get; set; }
public MySerializableSomething()
{
Colors = new List<string>();
}
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
while (reader.Read())
{
switch (reader.LocalName)
{
case "Id": Id = reader.ReadElementContentAsInt(); break;
case "Text": Text = reader.ReadElementContentAsString(); break;
case "Color": Colors.Add(reader.ReadElementContentAsString()); break;
}
}
}
public void WriteXml(XmlWriter writer)
{
writer.WriteElementString("Id", Id.ToString());
writer.WriteElementString("Text", Text);
writer.WriteStartElement("Colors");
foreach (var color in Colors)
{
writer.WriteElementString("Color", color);
}
writer.WriteEndElement();
}
}
|
Comparison between List, IList, and IEnumerable
By : Anthony Ecuman
Date : March 29 2020, 07:55 AM
hop of those help? IEnumerable is the base interface that the following extend or implement. It doesn't allow for direct access and is readonly. So use this only if you intend to iterate over the collection.
|
ASP.NET MVC cannot convert IList to IEnumerable? Also tried with IList to IList and still an error
By : Stepan Shykerynets
Date : March 29 2020, 07:55 AM
may help you . Just for fun here is a rewrite i think will do the trick for you, as well as its a little simpler. code :
public IList<Courses> GetHallamUniCourses()
{
return _hallamUniProxy.GetAllCourses().Select(a=>
new Courses()
{
CourseID = a.CourseId,
CourseName = a.CourseName,
CourseDescription = a.CourseDescription,
EntryRequirements = a.EntryCriteria
}).ToList();
}
//The name of the class must be Name+Controller, and it must extend Controller
class SomeController: Controller{
//This is a method in the controller, it returns a Result
//its URI is http[s]://Domain/Some
public ContentResult Index(){
return Content("Hello");
}
//This is a sub method of controller some, it's URI is http[s]://Domain/Some/Else
public ContentResult Else(){
return Content("Hello");
}
}
|
C# IEnumerable, ICollection, IList or List?
By : Rajath
Date : March 29 2020, 07:55 AM
should help you out It's kinda hard to answer this because it's very easy to post an opinion, and not an answer. I'll try though. This is a fundamental question about responsibility, more specifically, good API design.
|