linq-to-objects

Can I rewrite this more elegantly using LINQ?

自闭症网瘾萝莉.ら 提交于 2019-12-10 18:18:29
问题 I have a double[][] that I want to convert to a CSV string format (i.e. each row in a line, and row elements separated by commas). I wrote it like this: public static string ToCSV(double[][] array) { return String.Join(Environment.NewLine, Array.ConvertAll(array, row => String.Join(",", Array.ConvertAll(row, x => x.ToString()))); } Is there a more elegant way to write this using LINQ? (I know, one could use temporary variables to make this look better, but this code format better conveys what

Using linq to combine objects

橙三吉。 提交于 2019-12-10 18:06:25
问题 I have 2 instances of a class that implements the IEnumerable interface. I would like to create a new object and combine both of them into one. I understand I can use the for..each to do this. Is there a linq/lambda expression way of doing this? EDIT public class Messages : IEnumerable, IEnumerable<Message> { private List<Message> message = new List<Message>(); //Other methods } Code to combine MessagesCombined messagesCombined = new MessagesCombined(); MessagesFirst messagesFirst =

LINQ conditional aggregation based on next elements' values

三世轮回 提交于 2019-12-10 15:37:55
问题 What's a good LINQ equivalent of this pesudo-code: "given a list of strings, for each string that doesn't contain a tab character, concatenate it (with a pipe delimiter) to the end of the previous string, and return the resulting sequence" ? More Info: I have a List<string> representing lines in a tab-delimited text file. The last field in each line is always a multiline text field, and the file was generated by a buggy system that mishandles fields with embedded newlines. So I end up with a

Get duplicates for two columns with LINQ

本秂侑毒 提交于 2019-12-10 15:31:22
问题 LINQ drives me crazy. Why does following query not return the duplicates, whereas it works with only one identifier? Where is my error? ' generate some test-data ' Dim source As New DataTable source.Columns.Add(New DataColumn("RowNumber", GetType(Int32))) source.Columns.Add(New DataColumn("Value1", GetType(Int32))) source.Columns.Add(New DataColumn("Value2", GetType(Int32))) source.Columns.Add(New DataColumn("Text", GetType(String))) Dim rnd As New Random() For i As Int32 = 1 To 100 Dim

how to `.Take()` on a string and get a string at the end?

邮差的信 提交于 2019-12-10 12:28:24
问题 LINQ to Objects supports queries on string objects but when I use code such as below: string SomeText = "this is some text in a string"; return SomeText.Take(6).ToString(); All I get is: System.Linq.Enumerable+<TakeIterator>d__3a`1[System.Char] This is discussed as an "accident" in this question but this is what I am actually trying to do and I cannot find it through search anywhere. I know there are other ways to manipulate strings but then I also know you can do some really cool tricks with

How to do If statement in Linq Query

爱⌒轻易说出口 提交于 2019-12-10 10:26:23
问题 I currently have a list that contains the following CountryCode (string) CountryStr (string) RegionStr (string) RegionID (int) AreaStr (string) AreaID (int) This is a flattened set of linked data (so basically the results of a joined search that ive stored) The MVC route will only pass one string which I then need to match up to the data at the right level in the heirachy. So I'm trying to query the CountryStr then if it doesn't produce results the region then the area; but I need to do that

Does Enumerable.Where in LINQ-to-objects preserve order? [duplicate]

时光毁灭记忆、已成空白 提交于 2019-12-10 04:15:04
问题 This question already has answers here : Preserving order with LINQ (6 answers) Closed 6 years ago . var source = new List<string> { "A1", "A2", "B1", "B2" }; var filtered = source.Where(s => s.StartsWith("A")); foreach (var s in filtered) Console.WriteLine(s); // outputs first A1 and then A2 It seems like Enumerable.Where keeps the original order of elements when used on an ordered IEnumerable (such as a List<T> or T[] ). Is this always the case? If yes, where is this documented? 回答1:

Error in LINQ Left JOIN

跟風遠走 提交于 2019-12-10 02:32:08
问题 i have written below query in LINQ to perform left join but its throwing error: var qry = from c in dc.category_feature_Name_trans_SelectAll_Active() join p in dc.product_category_feature_trans_SelectAll() on c.cft_id equals p.cft_id into cp from p in cp.DefaultIfEmpty() select new { c.cft_id, c.feature_id, c.feature_name, p.product_id , p.value }; Error: Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web

How to use async lambda with SelectMany?

主宰稳场 提交于 2019-12-10 01:14:02
问题 I'm getting the following error when trying to use an async lambda within IEnumerable.SelectMany : var result = myEnumerable.SelectMany(async (c) => await Functions.GetDataAsync(c.Id)); The type arguments for method 'IEnumerable System.Linq.Enumerable.SelectMany(this IEnumerable, Func>)' cannot be inferred from the usage. Try specifying the type arguments explicitly Where GetDataAsync is defined as: public interface IFunctions { Task<IEnumerable<DataItem>> GetDataAsync(string itemId); }

Is it possible to compile a query for linq-to-objects

偶尔善良 提交于 2019-12-09 18:27:32
问题 I have a linq to objects query in a recursive loop and afraid when the objects approach more then 1000 and a have more then 100 users on the site -- my website will break. so is it possible to compile a linq to objects query. The linq query does nothing more then find the direct children of a node. 回答1: To see why the concept of compilation doesn't really make sense for LINQ to Object queries, it's useful to understand how LINQ is implemented. First, it should be clear that LINQ queries