linq-to-objects

DataTable Select vs LINQ Select

馋奶兔 提交于 2019-11-29 09:26:32
问题 Any advice on when DataTable.Select should be used versus LINQ Select when dealing with an in-memory DataTable? I find LINQ syntax easier and more powerful, but I'm not sure if there are performance or other issues which make a DataTable select preferable. (I'm using a third party API that provides a DataTable that has been pre-populated from the database. I need to filter that further in-memory.) 回答1: Based upon personal experience, I try to avoid the Datatable.Select. I find it to be slow

Cannot serialize parameter of type 'System.Linq.Enumerable… ' when using WCF, LINQ, JSON

醉酒当歌 提交于 2019-11-29 07:00:44
I have a WCF Service. It uses Linq-to-objects to select from a Dictionary. The object type is simple: public class User { public Guid Id; public String Name; } There is a collection of stored in a Dictionary<Guid,User> . I want to have a WCF OperationContract method like this: public IEnumerable<Guid> GetAllUsers() { var selection = from user in list.Values select user.Id; return selection; } It compiles fine, but when I run it I get: The server encountered an error processing the request. The exception message is 'Cannot serialize parameter of type 'System.Linq.Enumerable

How to get the first element of IEnumerable

时光毁灭记忆、已成空白 提交于 2019-11-29 06:17:48
问题 Is there a better way getting the first element of IEnumerable type of this: foreach (Image image in imgList) { picture.Width = (short)image.Columns; picture.Height = (short)image.Rows; break; } This is the exact declaration of the type: public class ImageList : IEnumerable, IDisposable 回答1: var firstImage = imgList.Cast<Image>().First(); 回答2: If you can't use LINQ you could also get the enumerator directly by imgList.GetEnumerator() And then do a .MoveNext() to move to the first element.

Distinct in LINQ with anonymous types (in VB.NET)

佐手、 提交于 2019-11-29 04:17:13
Supposing the referenced List below contains 2 elements: Dim Countries = From c In List _ Select New With { .Country = c.Country, .CountryID = c.CountryID } the code above returns .Country=Spain .CountryID = 1 .Country=Spain .CountryID = 1 How can i get the distinct values? The Countries query should contain only .Country=Spain .CountryID = 1 I can only assume you're dead set on the use of anonymous type as the answer given by Alex Peck is correct. (and I've upvoted it). However, this boils down to a VB.NET vs C# compiler discussion. In VB.NET, when an anonymous type is encountered only those

Use LINQ and C# to make a new List from an old List

。_饼干妹妹 提交于 2019-11-29 04:07:31
This should be pretty simple, but I am new at LINQ. I have a List<FillStruct> of FillList structs. I'd like to use LINQ to create a new List<NewFillStruct> where instead of having the number of buys and sells, I would have one variable containing the sum. For example, if the FillStruct structure has buy = 4 and sell = 2 then the NewFillStruct structure will have numlong = 2. If the FillStruct structure has buy = 2 and sell = 4 then the NewFillStruct structure will have numlong = -2. Here are the structures. struct FillStruct { int buy; int sell; string date; } struct NewFillStruct { int

Linq Group on Multiple Fields - VB.NET, Anonymous, Key

爱⌒轻易说出口 提交于 2019-11-29 01:26:04
I am stumped. I need help. I have a DTO object with duplicates patient address data. I need to get only the unique addresses. Dim PatientAddressDto = New List(Of PatientAddress) {Populate PatientAddressDto with lots of duplicate data} PatientAddressDto = (From d In PatientAddressDto Group d By PatientAddressDtoGrouped = New PatientAddress With { .Address1 = d.Address1, .Address2 = d.Address2, .City = d.City, .State = d.State, .Zip = d.Zip } Into Group Select New PatientAddress With { .Address1 = PatientAddressDtoGrouped.Address1, .Address2 = PatientAddressDtoGrouped.Address2, .City =

Linq cross join query for nested List

半城伤御伤魂 提交于 2019-11-28 12:43:17
问题 Please do help me out in one of the scenario, where I got stucked. It goes like this. A dynamically created List of table and List of Fields( inside Table ) using PropertyGrid. BindingList<Table> table = new BindingList<Table>(); [Serializable] [TypeConverter(typeof(TableConverter))] public class Table { private string _name = string.Empty; private HeaderCollection _hc = new HeaderCollection(); private BindingList<Fields> _fc = new BindingList<Fields>(); public Guid key; public Table() { key

LINQ OrderBy. Does it always return the same ordered list?

风流意气都作罢 提交于 2019-11-28 07:51:33
问题 I was trying out a simple OrderBy statement. The target data to order is something like below: [ {"id":40, "description":"aaa", "rate":1}, {"id":1, "description":"bbb", "rate":1}, {"id":4, "description":"ccc", "rate":2}, {"id":19, "description":"aaa", "rate":1} ] Then I order items by the rate property. The odd thing is that if I 'order' them, it 'skips' some items by a given offset and then 'take' only portion of the data. For example, var result = items.OrderBy(i => i.rate); var result =

Does .GroupBy() guarantee order in its groupings?

走远了吗. 提交于 2019-11-28 07:00:28
问题 Say I have an (ordered) sequence of animals: Eagle Elephant Tarantula Terrapin Tiger and I group by first letter: Animals.GroupBy(animal => animal.First()) will the elements of the IGrouping s in the resulting sequence be in the same order as the input sequence? 回答1: Yes, they will be: GroupBy (MSDN). The IGrouping<TKey, TElement> objects are yielded in an order based on the order of the elements in source that produced the first key of each IGrouping<TKey, TElement>. Elements in a grouping

C# Linq where clause as a variable

ⅰ亾dé卋堺 提交于 2019-11-28 05:42:22
I am trying to make a LINQ statement where the where clause comes from a variable. For example: string whereClause = address.zip == 23456; var x = from something in someList where whereClause; Is this possible? I cannot seem to get it to work. thanks, Update - my where clause is predefined and will be based on user input so I don't think this will work for me. Basically whereClause is not constructed in the method, it is a parameter of the method which does the LINQ. I didn't explain that well here is a better example: public void doLnq(string whereClause) { var x = from something in someList