linq-to-objects

how to find the longest string in a string[] using LINQ

心不动则不痛 提交于 2020-01-01 07:35:26
问题 I have an array of strings of variable length. Currently I have a loop that iterates through the array to find the longest string in array. Is there any way I could use LINQ to write it in more efficient and / or cleaner way? 回答1: It won't be much more efficient, however it would be a bit cleaner to do something like: var strings = new string[] { "1", "02", "003", "0004", "00005" }; string longest = strings.OrderByDescending( s => s.Length ).First(); Output: 00005 回答2: strings.Aggregate

LINQ to Entities / LINQ to SQL: switching from server (queryable) to client (enumerable) in the middle of a query comprehension?

a 夏天 提交于 2020-01-01 04:36:06
问题 In many cases, I want to do some filtering (and sometimes projection) on the server side and then switch to client-side for operations that the LINQ provider doesn't natively support. The naive approach (which is basically what I do now) is to just break it up into multiple queries, similar to: var fromServer = from t in context.Table where t.Col1 = 123 where t.Col2 = "blah" select t; var clientSide = from t in fromServer.AsEnumerable() where t.Col3.Split('/').Last() == "whatever" select t

LINQ query reuse and deferred execution

北慕城南 提交于 2019-12-31 03:59:31
问题 I was under the impression that I could create a LINQ query and then reuse it while change the parameters involved. But it seems that you cant change the source collection. Can someone give me a good explanation as to why, as I have clearly misunderstood something fundamental. Here is some example code. var source = Enumerable.Range(1, 10); var value = source.Where(x => x > 5); var first = value.ToArray(); source = Enumerable.Range(11, 20); var second = value.ToArray(); I was expecting first

Using LINQ to update string array

為{幸葍}努か 提交于 2019-12-30 21:41:27
问题 I'm trying to create a method to update an AssemblyInfo file with a new version string, using LINQ. I can successfully extract the string I need to update, but am not sure how to update the item in the collection. I'm new to LINQ, so any advice is appreciated! private void UpdateVersion(string file, string version) { string[] asmInfo = File.ReadAllLines(file); var line = asmInfo.Single(x => x.Trim().StartsWith("[assembly: AssemblyVersion")); line = "[assembly: AssemblyVersion\"" + version + "

Using LINQ to update string array

两盒软妹~` 提交于 2019-12-30 21:39:31
问题 I'm trying to create a method to update an AssemblyInfo file with a new version string, using LINQ. I can successfully extract the string I need to update, but am not sure how to update the item in the collection. I'm new to LINQ, so any advice is appreciated! private void UpdateVersion(string file, string version) { string[] asmInfo = File.ReadAllLines(file); var line = asmInfo.Single(x => x.Trim().StartsWith("[assembly: AssemblyVersion")); line = "[assembly: AssemblyVersion\"" + version + "

Check if all items are the same in a List

喜欢而已 提交于 2019-12-28 03:51:43
问题 I have a List(Of DateTime) items. How can I check if all the items are the same with a LINQ query? At any given time there could be 1, 2, 20, 50 or 100 items in the list. Thanks 回答1: Like this: if (list.Distinct().Skip(1).Any()) Or if (list.Any(o => o != list[0])) (which is probably faster) 回答2: I created simple extension method mainly for readability that works on any IEnumerable. if (items.AreAllSame()) ... And the method implementation: /// <summary> /// Checks whether all items in the

Checking for date overlap across multiple date range objects

柔情痞子 提交于 2019-12-25 05:12:08
问题 I have several records in a database that have Start and End Dates 09/15/2011 - 09/30/2011 10/15/2011 - 10/22/2011 11/01/2011 - 11/15/2011 When user stores a record, I need to make sure dates don't overlap. My simple code checks date ranges within a specific record (e.g. user enters 9/16/2011 or 10/21/2011, I throw an exception.) But, on the slim chance a user gets creative (e.g. 10/14/2011 - 10/23/2011 or even 10/14/2011 to 11/16/2011), now they have circumvented my check. BTW, the user

How to select Values from several tables

有些话、适合烂在心里 提交于 2019-12-25 04:40:48
问题 my three table structure are : USE [DB_OrderV2] GO /****** Object: Table [dbo].[tblPageInfo] Script Date: 07/24/2010 23:16:18 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[tblPageInfo]( [Code] [int] IDENTITY(1,1) NOT NULL, [PageID] [smallint] NOT NULL, [PageName] [nvarchar](80) NOT NULL, [Description] [nvarchar](80) NOT NULL, [Status] [tinyint] NOT NULL, [CreateUserID] [smallint] NULL, [CreateDate] [smalldatetime] NULL, [UpdateUserID] [smallint] NULL,

Need help creating Linq.Expression to Enumerable.GroupBy

孤街浪徒 提交于 2019-12-25 01:46:39
问题 I am attempting to generate an Expression tree that ultimately calls a series of GroupBy methods on the Enumerable type. In simplified form I am attempting something like this: IEnumerable<Data> list = new List<Data>{new Data{Name = "A", Age=10}, new Data{Name = "A", Age=12}, new Data{Name = "B", Age=20}, new Data{Name="C", Age=15}}; Expression data = Expression.Parameter(typeof(IEnumerable<Data>), "data"); Expression arg = Expression.Parameter(typeof(Data), "arg"); Expression nameProperty =

LINQ to JSon response

痴心易碎 提交于 2019-12-24 19:43:45
问题 I have posted my class below and with sample data. I need to pass this class as an json response. [Serializable] public class Student { public string Fname {get;set;} public string FirstSemMarkcsv {get;set;} //This should hold marks as comma separated values. public string SecondSemMarkcsv {get;set;} //This should hold marks as comma separated values. } Sample Data Fname FirstSemMark SecondSemMark John 95 90 John 80 93 John 70 80 Peter 60 78 Peter 80 80 Peter 90 85 I need to pass this as json