linq

Null coalesce not working in LINQ query

穿精又带淫゛_ 提交于 2021-02-18 12:45:08
问题 Take this: int? item1 = null; int? item2 = null; someObjectList.Where(x => x.SomeItem1 == (item1 ?? x.SomeItem1) && x.SomeItem2 == (item2 ?? x.SomeItem2) ); Where someObjectList is not empty and SomeItem1 and SomeItem2 is null in all the objects in the list. Why is it returning nothing? EDIT: My Code: public void GetPlacementsByMaterial(long clientMaterialID) { ClientMaterial clientMaterial = ((ApplicationEntityModel)NavigationItem.ObjectContext).ClientMaterial.FirstOrDefault(x => x

Sorting using property name as string

倖福魔咒の 提交于 2021-02-18 12:25:08
问题 I would like my Web API to be able to sort its output by a string parameter such as this one: http://myapi.com/api/people?skip=0&take=50&orderBy=lastName&descending=true . Because I also have pagination support (with skip and take ) in my API, I would like the orderBy and descending parameter to be applied to the SQL query directly, so that the correct result comes from the database. When doing this however, the code can become very hard to manage when trying to match the parameters for

LINQ: Selecting items from a list (Group By/Select/Sum & Max!)

这一生的挚爱 提交于 2021-02-18 05:50:49
问题 Just getting my head around Linq and having lots of fun! Can any one aid me with a query for this: I have a list of data: Key Value Aaa 12 AaA 10 AAa 5 BBB 2 Bbb 1 1. I want to group by Key.ToUpper() 2. For every group I need the Max(Value) & Sum(Value) 3. For every group I want to select the entries There the Value != Max(value) the final result should be like this: Key Max Total AaA 12 27 AAa 12 27 Bbb 2 3 Thanks! Update, actually I also need the Key from the Maximum entry: Key Max Total

LINQ: Selecting items from a list (Group By/Select/Sum & Max!)

≡放荡痞女 提交于 2021-02-18 05:50:25
问题 Just getting my head around Linq and having lots of fun! Can any one aid me with a query for this: I have a list of data: Key Value Aaa 12 AaA 10 AAa 5 BBB 2 Bbb 1 1. I want to group by Key.ToUpper() 2. For every group I need the Max(Value) & Sum(Value) 3. For every group I want to select the entries There the Value != Max(value) the final result should be like this: Key Max Total AaA 12 27 AAa 12 27 Bbb 2 3 Thanks! Update, actually I also need the Key from the Maximum entry: Key Max Total

Why does it hang indefinitely?

寵の児 提交于 2021-02-17 07:07:58
问题 I wonder why is the reason why this code never finishes its execution. It makes use of MoreLinq void Main() { var values = MoreEnumerable.Random(1, 200); var filtered = MyMethod(values) .Take(2) .Dump(); } public IEnumerable<int> MyMethod(IEnumerable<int> source) { return source .Select(x => new[] { x }) .Aggregate((a, b) => new[] { a.Last() + b.First()}); } 回答1: Because MoreEnumerable.Random(1, 200) returns an infinite sequence and the .Aggregate statement in MyMethod is trying to enumerate

How to update a global variable inside `where` clause in LINQ?

佐手、 提交于 2021-02-17 06:53:07
问题 I want to filter a list using LINQ with Where extension method. But apart from filtering I also want to update a global variable inside Where . However I cannot do it. Consider this example: var list = new List<string> { "1", "2", "3", "4", "5" }; bool flag = false; var newList = list.Where(item => { flag = true; return item == "2"; }); // Here I expect flag = true, but in fact it's false Console.Write(flag); As you can see I set flag = true , still the value flag == false after execution. It

Implementation of [PARTITION BY] command by LINQ

妖精的绣舞 提交于 2021-02-17 06:06:31
问题 I did my best search to convert PARTION BY command from TSQL to LINQ command. But apparently there is no way to convert it. This is my code to which I am going to convert: WITH MyRowSet AS ( SELECT OrderDate ,SalesOrderNumber ,AccountNumber ,CustomerID ,ROW_NUMBER() OVER (PARTITION BY CustomerID ORDER BY CustomerID, OrderDate DESC) AS RowNum FROM [Sales].[SalesOrderHeader] ) SELECT * FROM MyRowSet WHERE RowNum = 1 If exist any solution I would be greatful to know. 回答1: linq2db has this

Null ref when calling .ToList() within a Select function

拟墨画扇 提交于 2021-02-17 05:18:50
问题 in my .NET Core 3.1 application, I am getting a null reference exception when I include .ToList() on the following emailAddress =... line: var customersTask = eshop.TieredCustomer .Where(customer => customer != null) .Where(customer => customer.SendPriceListAutomatically.HasValue && customer.SendPriceListAutomatically.Value && customer.DistributionList.HasValue && !string.IsNullOrWhiteSpace(customer.Emails)) .Select(customer => new Customer { distributionList = customer.DistributionList.Value

LINQ aggregate items in a list, concatenate a property

五迷三道 提交于 2021-02-17 04:09:10
问题 Please be kind to a LINQ dummy... Say I have 8 rows data like this: +-------------------+ | ID WORKTYPEDESC| +-------------------+ | 1 plumber | | 1 carpenter | | 1 electrician | | 2 juggler | | 2 mime | | 3 writer | | 3 actor | +-------------------+ As an 8-item IList<Worktype> collection where Worktype looks like public class Worktype { public int Id { get; set; } public string WorktypeDesc { get; set; } } And what I want is to aggregate by Id to get a list with 3 Worktypes, each with a

LINQ aggregate items in a list, concatenate a property

六月ゝ 毕业季﹏ 提交于 2021-02-17 04:09:09
问题 Please be kind to a LINQ dummy... Say I have 8 rows data like this: +-------------------+ | ID WORKTYPEDESC| +-------------------+ | 1 plumber | | 1 carpenter | | 1 electrician | | 2 juggler | | 2 mime | | 3 writer | | 3 actor | +-------------------+ As an 8-item IList<Worktype> collection where Worktype looks like public class Worktype { public int Id { get; set; } public string WorktypeDesc { get; set; } } And what I want is to aggregate by Id to get a list with 3 Worktypes, each with a