generic-list

sorting a generic list of doubles

99封情书 提交于 2019-12-01 04:28:33
问题 I have a generic list of doubles that show on the page like this: 1199.17 1199.17 1161.67 1161.67 1161.67 1161.67 1161.67 1161.67 1161.67 1161.67 1161.67 1161.67 1161.67 1161.67 1161.67 1199.17 1349.17 1349.17 1349.17 1349.17 1349.17 1349.17 1311.67 1311.67 1311.67 1311.67 1311.67 1349.17 2174.17 2174.17 2174.17 2174.17 2136.67 2136.67 2136.67 2136.67 2174.17 2361.67 2361.67 2361.67 2361.67 2361.67 2361.67 2361.67 2361.67 2399.17 2849.17 2849.17 2849.17 2849.17 2849.17 2849.17 2849.17 2849.17

Loop implementation of List.Contains() appears faster than the built-in one. Is it? If so, why?

落爺英雄遲暮 提交于 2019-11-30 23:45:02
问题 (This question arises from a discussion that started here) I was comparing the timings for looking for a true value in a List<bool> using List.Contains() with those for a hand-rolled loop. I am seeing different results from those reported by other people. I have tried it on several systems, and the loop seems faster by between 2 and 3.5 times on all the systems I've tried it on. These systems range from 5-year-old laptops running XP with .Net 4 to recent PCs running Windows 8 and .Net 4.5.

NUnit comparing two lists

ε祈祈猫儿з 提交于 2019-11-30 16:29:11
问题 OK so I'm fairly new to unit testing and everything is going well until now. I'm simplifying my problem here, but basically I have the following: [Test] public void ListTest() { var expected = new List<MyClass>(); expected.Add(new MyOtherClass()); var actual = new List<MyClass>(); actual.Add(new MyOtherClass()); Assert.AreEqual(expected,actual); //CollectionAssert.AreEqual(expected,actual); } But the test is failing, shouldn't the test pass? what am I missing? 回答1: I convert my comment to

Export a C# List of Lists to Excel

不羁的心 提交于 2019-11-30 15:51:35
问题 Using C#, is there a direct way to export a List of Lists (i.e., List<List<T>> ) to Excel 2003? I am parsing out large text files and exporting to Excel. Writing one cell at a time creates way too much overhead. I chose to use List<T> so that I would not have to worry about specifying the number of rows or columns. Currently, I wait until end of file, then put the contents of my List<List<object>> into a 2-dimensional array. Then the array can be set as the value of an Excel.Range object. It

How can I sort List<T> based on properties of T?

隐身守侯 提交于 2019-11-30 12:44:23
问题 My Code looks like this : Collection<NameValueCollection> optionInfoCollection = .... List<NameValueCollection> optionInfoList = new List<NameValueCollection>(); optionInfoList = optionInfoCollection.ToList(); if(_isAlphabeticalSoting) Sort optionInfoList I tried optionInfoList.Sort() but it is not working. 回答1: Using the sort method and lambda expressions, it is really easy. myList.Sort((a, b) => String.Compare(a.Name, b.Name)) The above example shows how to sort by the Name property of your

Jersey can produce List<T> but cannot Response.ok(List<T>).build()?

回眸只為那壹抹淺笑 提交于 2019-11-30 11:02:44
问题 Jersey 1.6 can produce: @Path("/stock") public class StockResource { @GET @Produces(MediaType.APPLICATION_JSON) public List<Stock> get() { Stock stock = new Stock(); stock.setQuantity(3); return Lists.newArrayList(stock); } } But cannot do the same with: @Path("/stock") public class StockResource { @GET @Produces(MediaType.APPLICATION_JSON) public Response get() { Stock stock = new Stock(); stock.setQuantity(3); return Response.ok(Lists.newArrayList(stock)).build(); } } Giving the error: A

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

丶灬走出姿态 提交于 2019-11-30 09:04:16
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 online deal with Lists of primitives (strings/ints) and are not especially helpful. I would say: var

How can I sort List<T> based on properties of T?

依然范特西╮ 提交于 2019-11-30 03:38:21
My Code looks like this : Collection<NameValueCollection> optionInfoCollection = .... List<NameValueCollection> optionInfoList = new List<NameValueCollection>(); optionInfoList = optionInfoCollection.ToList(); if(_isAlphabeticalSoting) Sort optionInfoList I tried optionInfoList.Sort() but it is not working. Using the sort method and lambda expressions, it is really easy. myList.Sort((a, b) => String.Compare(a.Name, b.Name)) The above example shows how to sort by the Name property of your object type, assuming Name is of type string. If you just want Sort() to work, then you'll need to

C# Update combobox bound to generic list

随声附和 提交于 2019-11-30 02:11:00
问题 I have a combobox on my form that is bound to a generic list of string like this: private List<string> mAllianceList = new List<string>(); private void FillAllianceList() { // Add alliance name to member alliance list foreach (Village alliance in alliances) { mAllianceList.Add(alliance.AllianceName); } // Bind alliance combobox to alliance list this.cboAlliances.DataSource = mAllianceList; } The user may then add or remove items in the combobox. I have read elsewhere that by simply adding or

Jersey can produce List<T> but cannot Response.ok(List<T>).build()?

无人久伴 提交于 2019-11-29 23:02:31
Jersey 1.6 can produce: @Path("/stock") public class StockResource { @GET @Produces(MediaType.APPLICATION_JSON) public List<Stock> get() { Stock stock = new Stock(); stock.setQuantity(3); return Lists.newArrayList(stock); } } But cannot do the same with: @Path("/stock") public class StockResource { @GET @Produces(MediaType.APPLICATION_JSON) public Response get() { Stock stock = new Stock(); stock.setQuantity(3); return Response.ok(Lists.newArrayList(stock)).build(); } } Giving the error: A message body writer for Java class java.util.ArrayList, and Java type class java.util.ArrayList, and MIME