linq-to-objects

Coalescing results in Linq

回眸只為那壹抹淺笑 提交于 2019-12-24 11:19:12
问题 have looked through many posts here on SO, and haven't found any that address this. Just a note that all code presented here is simplified but representative of the real code. I have a data table that describes some properties of coverage plans. The query to bring back the best match looks something like this: select coalesce ( (select c.PercentOfCoverageA from CoveragePlans c where c.coverage = :COVERAGE and c.plancode = :PLANCODE and c.statecode = :STATECODE), (select c.PercentOfCoverageA

Sorting a JSON Object using Linq

本小妞迷上赌 提交于 2019-12-24 05:09:05
问题 I am getting a response back from a Google Search Appliance suggest service in the form of JSON in the following format string jsonString = @"{ ""query"": ""t"", ""results"": [ { ""name"": ""tom"", ""type"": ""suggest"" }, { ""name"": ""tim"", ""type"": ""suggest"" }]}"; I want to sort the results list by name alphabeticaly and change the names to sentence case. I can do this in jquery but would prefer to do it on the server side for performance reasons. I can sort the results but that

LINQ Query Help

我是研究僧i 提交于 2019-12-24 02:14:23
问题 I have this query which runs a join on Books, TradingDesks and ProductInfos. The data is huge in each of this collection. var queryJoin = from b in books.Values join d in tradingDesks.Values on b.TradingDeskId equals d.Id join p in ProductInfos.Values **on b.Id equals p.RiskBookId** select new { p, Book = b.Name, TradingDeskName = d.Name }; In the highlighted line (on b.Id equals p.RiskBookId) , I also want to add another condition like, (on b.Id equals p.RiskBookId || p.RiskBookId == 0) .

How can I organize this data into the objects I want with LINQ?

旧城冷巷雨未停 提交于 2019-12-24 01:09:34
问题 I have an the following class I'm trying to create a list of with a bunch of data I have queried: public class Agency { public string AgencyName { get; set; } public int AgencyID { get; set; } public IEnumerable<AuditRule> rules { get; set; } } Where "AuditRule" is: public class AuditRule { public int AuditRuleID { get; set; } public string AuditRuleName { get; set } public int AvgDaysWorked { get; set; } } Right now, after a fairly complicated query I have been able to get my data into an

LINQ to Objects question

限于喜欢 提交于 2019-12-24 00:54:06
问题 I am writing a method that is passed a List<AssetMovements> where AssetMovements looks something like public class AssetMovements { public string Description { get; set; } public List<DateRange> Movements { get; set; } } I want to be able to flatten out these objects into a list of all Movements regardless of Description and am trying to figure out the LINQ query I need to do this. I thought that from l in list select l.Movements would do it and return IEnumerable<DateRange> but instead it

How to combine three lists of objects by primary key using linq

扶醉桌前 提交于 2019-12-23 17:52:11
问题 I'm trying to combine 3 lists of objects. I have a person list, address list and an addressRelation list. I want to combine these lists into a new list ordered by person.id, use it as a datasource for a listview, and then be able to access the properties in the aspx page. Is this possible? 回答1: Roughly using System.Linq; class Person { public int Id; public string Name; } class Address { public int Id; public string Street; } class PersonAddress { public int PersonId, AddressId; } public

Can I use LINQ to strip repeating spaces from a string?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-23 15:29:31
问题 A quick brain teaser: given a string This is a string with repeating spaces What would be the LINQ expressing to end up with This is a string with repeating spaces Thanks! For reference, here's one non-LINQ way: private static IEnumerable<char> RemoveRepeatingSpaces(IEnumerable<char> text) { bool isSpace = false; foreach (var c in text) { if (isSpace && char.IsWhiteSpace(c)) continue; isSpace = char.IsWhiteSpace(c); yield return c; } } 回答1: Since nobody seems to have given a satisfactory

Linq skip first where (linq to objects)

穿精又带淫゛_ 提交于 2019-12-23 14:40:37
问题 I have the need to skip the first element that matches a predicate as a linq query. So far as I can tell the best I can do is something like this: var list = new [] {1,2,3,4,5,6,7,8,9}; var skippedFirstOdd = false; var skipFirstEvenNumber = list.SkipWhile(number => { if(number % 2 != 0) { skippedFirst = true; return true; } else { return false; } }); Which works (I think), but isn't very elegant. Is there a cleaner way to achieve this? 回答1: You could write an iterator-block extension method:

How to define IEnumerable behavior by contract?

主宰稳场 提交于 2019-12-23 12:53:37
问题 Consider this 2 methods that returns IEnumerable: private IEnumerable<MyClass> GetYieldResult(int qtResult) { for (int i = 0; i < qtResult; i++) { count++; yield return new MyClass() { Id = i+1 }; } } private IEnumerable<MyClass> GetNonYieldResult(int qtResult) { var result = new List<MyClass>(); for (int i = 0; i < qtResult; i++) { count++; result.Add(new MyClass() { Id = i + 1 }); } return result; } This code shows 2 different behaviors when calling some method of IEnumerable: [TestMethod]

How to define IEnumerable behavior by contract?

点点圈 提交于 2019-12-23 12:53:28
问题 Consider this 2 methods that returns IEnumerable: private IEnumerable<MyClass> GetYieldResult(int qtResult) { for (int i = 0; i < qtResult; i++) { count++; yield return new MyClass() { Id = i+1 }; } } private IEnumerable<MyClass> GetNonYieldResult(int qtResult) { var result = new List<MyClass>(); for (int i = 0; i < qtResult; i++) { count++; result.Add(new MyClass() { Id = i + 1 }); } return result; } This code shows 2 different behaviors when calling some method of IEnumerable: [TestMethod]