ilist

I have a list with over a million objects in it, trying to find the fastest way to search through it

限于喜欢 提交于 2021-01-28 04:46:01
问题 I have a list that stores well over a million objects within it. I need to look through the list and update the objects found through the below query as efficiently as possible. I was thinking of using a Dictionary or HashSet, but I'm relatively new to C# and could not figure out how to implement these other two approaches. My current code is simply a LINQ statement searching through an IList. public IList<LandObject> landObjects = new List<LandObject>(); var lObjsToUpdate = from obj in

How Do I Sort IList<Class>?

混江龙づ霸主 提交于 2020-08-19 06:16:19
问题 There's no Sort() function for IList . Can someoene help me with this? I want to sort my own IList. Suppose this is my IList: public class MyObject() { public int number { get; set; } public string marker { get; set; } } How do I sort myobj using the marker string? public void SortObject() { IList<MyObject> myobj = new List<MyObject>(); } 回答1: Use OrderBy Example public class MyObject() { public int number { get; set; } public string marker { get; set; } } IList<MyObject> myobj = new List

Convert IList to array in C#

大憨熊 提交于 2020-06-08 07:00:13
问题 I want to convert IList to array: Please see my code: IList list = new ArrayList(); list.Add(1); Array array = new Array[list.Count]; list.CopyTo(array, 0); Why I get System.InvalidCastException : At least one element in the source array could not be cast down to the destination array type ? How that can be resolved assuming I can not use ArrayList as type for list variable ? Update 1: I use .NET 1.1. So I can not use Generics, Linq and so on. I just want to receive result for the most common

IList using covariance and contravariance in c#, is this possible?

依然范特西╮ 提交于 2020-02-03 05:17:45
问题 would this be possible? (I don't have vs. 2010, so I can't try it myself, sorry) public interface IComplexList<out TOutput, in TInput> where TOutput : TInput { public IEnumerator<TOutput> GetEnumerator(); public void Add(TInput item); } public interface IList<T> : IComplexList<T, T> { } If I get it right, you could use this to actually implement covariance and contravariance in the same interface. 回答1: No, you can't. In your example IList<T> is invariant. IList<T> would require to declare in

Which Json deserializer renders IList<T> collections?

蹲街弑〆低调 提交于 2020-01-12 04:06:12
问题 I'm trying to deserialize json to an object model where the collections are represented as IList<T> types. The actual deserializing is here: JavaScriptSerializer serializer = new JavaScriptSerializer(); return serializer.Deserialize<IList<Contact>>( (new StreamReader(General.GetEmbeddedFile("Contacts.json")).ReadToEnd())); Before i post the exception i'm getting you should know what the implicit conversions are. This is the Contact type: public class Contact { public int ID { get; set; }

IList<int> throws Null Reference Exception when adding values

混江龙づ霸主 提交于 2020-01-07 01:59:13
问题 I have a class: public class ClientModelData { public int clientID { get; set; } public IList<int> LocationIDs { get; set; } } When I call it: ClientModelData obj = new ClientModelData(); obj.LocationIDs.Add(1); It throws an exception: `((System.Collections.Generic.ICollection<int>)(client.LocationID))' is null` 回答1: LocationIDs is not initialized therefore it is giving you the error. public IList<int> LocationIDs { get; set; } You should create an instance in the constructor public

Sorting an IList in C#

*爱你&永不变心* 提交于 2019-12-27 17:06:24
问题 So I came across an interesting problem today. We have a WCF web service that returns an IList. Not really a big deal until I wanted to sort it. Turns out the IList interface doesn't have a sort method built in. I ended up using the ArrayList.Adapter(list).Sort(new MyComparer()) method to solve the problem but it just seemed a bit "ghetto" to me. I toyed with writing an extension method, also with inheriting from IList and implementing my own Sort() method as well as casting to a List but

Cast an IList to an IList<t> using dynamic instantiation

纵然是瞬间 提交于 2019-12-25 12:04:51
问题 I am try to convert the following NHibernate query using dyanmic instantiation into an IList<t> rather than an IList. IList<AllName> allNames = (IList<AllName>)Session.CreateQuery( @"select new AllName( name.NameId, name.FirstName, origin.OriginId, origin.Description, name.Sex, name.Description, name.SoundEx ) from Name name join name.Origin origin") .SetFirstResult(skip) .SetMaxResults(pageSize); Running this I get the following error:- Unable to cast object of type 'NHibernate.Impl

Cast an IList to an IList<t> using dynamic instantiation

会有一股神秘感。 提交于 2019-12-25 12:04:41
问题 I am try to convert the following NHibernate query using dyanmic instantiation into an IList<t> rather than an IList. IList<AllName> allNames = (IList<AllName>)Session.CreateQuery( @"select new AllName( name.NameId, name.FirstName, origin.OriginId, origin.Description, name.Sex, name.Description, name.SoundEx ) from Name name join name.Origin origin") .SetFirstResult(skip) .SetMaxResults(pageSize); Running this I get the following error:- Unable to cast object of type 'NHibernate.Impl