linq

How Order by Case in LINQ

故事扮演 提交于 2021-02-05 08:41:29
问题 I have this answer Entity framework OrderBy "CASE WHEN" but this only handle two options var p = ctx.People.OrderBy(p => (p.IsQualityNetwork == 1 || p.IsEmployee == 1) ? 0 : 1) .ThenBy(p => p.Name); I have a text field and want rows appear on an specific order. In sql I would do: ORDER BY CASE when name = "ca" then 1 when name = "po" then 2 when name = "pe" then 3 when name = "ps" then 4 when name = "st" then 5 when name = "mu" then 6 END 回答1: How about creating a map of your sort order? var

How to find a sub-list of two consecutive items in a list using Linq

Deadly 提交于 2021-02-05 08:15:46
问题 Suppose that we have a list of strings like "A", "B", "C", "D", "E", "F" Now I'd like to search for a sub-list of two consecutive items D E in this list. How can I do this using Linq? my approach is: int i = list.FindIndex(x => x == "D"); int j = list.FindIndex(x => x == "E"); int p = i < 0 || j < 0 ? -1 : (j == i + 1 ? i : -1); Is it a correct solution? Is there any shorter solution? 回答1: You could rewrite your approach as follows: bool hasSublist = list .SkipWhile(x => x != "D") .Take(2)

Visual Studio 2015 using Linq in conditional breakpoint

不想你离开。 提交于 2021-02-05 07:15:26
问题 Is it possible to use Linq within a conditional breakpoint? I'm attempting to break when the following condition is true: parentElement.ChildElements.Any(c => c.Id == 1) When ever the debugger is hit the following error message is displayed The debugger is unable to evaluate this expression. I have tried the following condition in case the issue was related to using .Any() parentElement.ChildElements.Where(c => c.Id == 1).Count() > 0 This resulted in the same error as above being displayed. I

Visual Studio 2015 using Linq in conditional breakpoint

孤街浪徒 提交于 2021-02-05 07:14:31
问题 Is it possible to use Linq within a conditional breakpoint? I'm attempting to break when the following condition is true: parentElement.ChildElements.Any(c => c.Id == 1) When ever the debugger is hit the following error message is displayed The debugger is unable to evaluate this expression. I have tried the following condition in case the issue was related to using .Any() parentElement.ChildElements.Where(c => c.Id == 1).Count() > 0 This resulted in the same error as above being displayed. I

Find object in list with given property value then find dictionary value

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-05 07:01:26
问题 I have a list of objects. Each of these objects has a Name property, and an ObservablePairCollection which is just a custom dictionary that works EXACTLY like a dictionary, has a key/value pair. Given two strings, one for name and one for key, I want to find the object that first matches the name given, and then selects the pair from that model's dictionary that matches the given key value. Example: Given the string "model1" for name, and "Latitude" for the key, an object who's name property

Linq outer join using inequality?

走远了吗. 提交于 2021-02-05 05:48:10
问题 In SQL I'd say: select a.* from TableA a left join TableB b on a.Type = b.Type and a.SomeDate < b.AnotherDate where b.ID is null This would select all records in TableA where no record exists in TableB of the same Type and later date. In Linq, how do you do this? from a in TableA join b in TableB on a.Type equals b.Type into j // what about the comparator? from x in j.DefaultIfEmpty() where x == null select a; Thanks! EDIT: A few good answers have been proposed, all of which address the

Why do changes made in foreach to a Linq grouping select get ignored unless I add ToList()?

送分小仙女□ 提交于 2021-02-05 05:29:45
问题 I have the following method. public IEnumerable<Item> ChangeValueIEnumerable() { var items = new List<Item>(){ new Item("Item1", 1), new Item("Item2", 1), new Item("Item3", 2), new Item("Item4", 2), new Item("Item5", 3) }; var groupedItems = items.GroupBy(i => i.Value) .Select(x => new Item(x.First().Name, x.Key)); foreach (var item in groupedItems) { item.CalculatedValue = item.Name + item.Value; } return groupedItems; } Into the groupedItems collection the CalculatedValue s are null.

Filtering list of objects in datagridview based on cell value

喜夏-厌秋 提交于 2021-02-05 05:23:24
问题 I am currently unsure on the best way of going about getting a filter to work right on a datagridview that has its datasource set to a list of objects. So given an object of: public class DepositAccountBill { #region Properties public int AccountBillID { get; set; } public int AccountID { get; set; } public string AccountNumber { get; set; } public string ControlNumber { get; set; } public DateTime BillDate { get; set; } public decimal DepositAmount { get; set; } } I have a datagridview table

Filtering list of objects in datagridview based on cell value

廉价感情. 提交于 2021-02-05 05:22:05
问题 I am currently unsure on the best way of going about getting a filter to work right on a datagridview that has its datasource set to a list of objects. So given an object of: public class DepositAccountBill { #region Properties public int AccountBillID { get; set; } public int AccountID { get; set; } public string AccountNumber { get; set; } public string ControlNumber { get; set; } public DateTime BillDate { get; set; } public decimal DepositAmount { get; set; } } I have a datagridview table

How to make an anonymous types property name dynamic?

不问归期 提交于 2021-02-05 05:21:16
问题 I have a following LinqToXml query: var linqDoc = XDocument.Parse(xml); var result = linqDoc.Descendants() .GroupBy(elem => elem.Name) .Select(group => new { TagName = group.Key.ToString(), Values = group.Attributes("Id") .Select(attr => attr.Value).ToList() }); Is it possible somehow to make the field of my anonymous type it to be the variable value, so that it could be as (not working): var linqDoc = XDocument.Parse(xml); var result = linqDoc.Descendants() .GroupBy(elem => elem.Name)