generic-list

convert .NET generic List to F# list

橙三吉。 提交于 2019-12-21 03:08:27
问题 Is there a built-in method to convert the .NET List<> into the F# list? 回答1: Try List.ofSeq in the Microsoft.FSharp.Collections namespace. # List.ofSeq : seq<'T> -> 'T list It's not specifically for System.Collections.Generic.List<T> , but for IEnumerable<T> ( seq<'T> in F#) types in general, so it should still work. (It's also not strictly built into the F# language, but neither is List<T> built into C# or VB.NET. Those are all part of the respective standard libraries.) 回答2: Given

Whats the 'modern' way to find common items in two Lists<T> of objects?

时光毁灭记忆、已成空白 提交于 2019-12-18 13:13:07
问题 I have two Generic Lists containing different types, for the sake of example, lets call them Products and Employees . I'm trying to find Products that are based at the same location as Employees, i.e. where product.SiteId == emp.SiteId List<Product> lstProds; List<Employees> lstEmps; My (old skool) brain is telling me to use a forEach loop to find the matches but I suspect there is a ('better'/terser/faster?) way to do it using Linq. Can anyone illuminate me? All the examples I've found

How to convert DataTable to object type List in C# [closed]

本小妞迷上赌 提交于 2019-12-17 19:29:53
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . I want to convert DataTable to List<object> in C#. Here is my code. But it is not working. Please help me public List<object> ShowMessage() { List<object> obj = new List<object>(); DataTable dt = new DataTable();

How to instantiate a generic list out of an unkown type? [duplicate]

别等时光非礼了梦想. 提交于 2019-12-13 11:24:58
问题 This question already has answers here : How to dynamically create generic C# object using reflection? [duplicate] (5 answers) Closed 6 years ago . I have an object as a parameter and I want to make a generic list out of that object type. Most of times I want to send a custom object type. This code declares the concept of my problem. public void myMethod(object myObject) { List<typeof(myObject)> _NewList=new List<typeof(myObject)(); } 回答1: You could make the method generic: public void

getEntity with GenericType

試著忘記壹切 提交于 2019-12-13 04:48:41
问题 Trying to get an Entity from a REST service. My code is like this: I want to send in an object like this: new GenericType<List<MyObject>>() {} to a methode like this: public static Object callRestWithUrl(String url, Class<?> responseObject) throws MetaServiceException { ClientResponse response = RESTConnectionUtils.callMetaService(url); result = response.getEntity(responseObject); ..... But it gets evaluated to List during runtime and not GenericType and an exception is thrown. 回答1: How do

Example to use a hashcode to detect if an element of a List<string> has changed C#

无人久伴 提交于 2019-12-12 23:31:37
问题 I have a List that updates every minute based on a Linq query of some XML elements. the xml changes, from time to time. It was suggested to me that I could use Hashcode to determine if any of the strings in the list have changed. I have seen some examples of Md5 hashcode calculations for just a string, but not for a list...could someone show me a way of doing this with a list? I tried something simple like int test = list1.GetHashCode; but the code is the same no matter what is in the list...

How to simulate List<T>.Any method via MethodInfo?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 23:03:56
问题 I try to simulate List<T>.Any method via MethodInfo . First I define a class: class Test { int _value; public int Value { get; set; } public Test(int v) { _value = v; } } ..and then I create a List<Test> List<C> list = new List<C>(); for (int i = 0; i < 10; i++) { list.Add(new C(i)); } My aim to call list.Any(c=>c.Value>3) via MethodInfo, now I met the problem about how to locate the real method. I can find Any() method in System.Linq.Enumerable and System.Linq.Queryable . When I check the

How to pass List<DerivedClass> when param type is List<BaseClass>?

感情迁移 提交于 2019-12-12 07:26:58
问题 How can i pass a list which is a list of DerivedObjects where the Method is expecting a list of BaseObjects. I am converting the list .ToList<BaseClass>() and am wondering if there is a better way. My second problem is the syntax is incorrect. I am trying to pass the list byref and i am getting an error: 'ref' argument is not classified as a variable How can I fix these two problem? thanks. public class BaseClass { } public class DerivedClass : BaseClass { } class Program { static void Main

Given System.Type T, Deserialize List<T>

倖福魔咒の 提交于 2019-12-12 05:48:42
问题 I have a number of classes that I want to serialize and de-serialize. I am trying to create a function that, given a type ("User", "Administrator", "Article", etc) will de-serialize the file with the list of those items. For example: /* I want to be able to do this */ List<Article> allArticles = GetAllItems(typeof(Article)); I cannot figure out how to achieve the above, but I managed to get this working: /* BAD: clumsy method - have to pass a (typeof(List<Article>)) instead of typeof(Article)

How to iterate over items of a list when I only have access to the object of that list and dont know the type parameter?

不羁岁月 提交于 2019-12-12 03:03:50
问题 Somewhere in my code I have an object that I already know that is a list. But I don't know the type parameter of that list. I need to iterate over it's items. I tried to cast that object to a list of objects but it didn't help me: List<Object> objList = (List<Object>)(dataModel.Value); foreach (var item in objList) { Console.WriteLine(item.ToString()); } In the above code, the Value property of dataModel is a list of XYZ values, but it throws an exception when I run this code. It says that,