linq-to-objects

LINQ return items in a List that matches any Names (string) in another list

浪尽此生 提交于 2019-11-28 04:41:26
I have 2 lists. 1 is a collection of products. And the other is a collection of products in a shop. I need to be able to return all shopProducts if the names match any Names in the products. I have this but it doesn't seem to work. Any ideas? var products = shopProducts.Where(p => p.Name.Any(listOfProducts. Select(l => l.Name).ToList())).ToList(); I need to say give me all the shopproducts where name exists in the other list. var products = shopProducts.Where(p => listOfProducts.Any(l => p.Name == l.Name)) .ToList(); For LINQ-to-Objects, if listOfProducts contains many items then you might get

Linq nested list expression

倾然丶 夕夏残阳落幕 提交于 2019-11-28 02:57:16
问题 please I need your help with a Linq expression: I have nested objects with lists, this is how the main object hierarchy looks like (each dash is an atribute of the sub-class): Folder -name -List<Subfolder> Subfolders -name -List<Document> Documents -name -key Having this hierarchy of objects, I have a Document name, and I want to search for it and return its parent folder (Subfolder) Example: Folder -name: Customer -List<Subfolder> Subfolders -name: Personal -List<Document> Documents -name:

Parent/Children Xml to DTO Object Model with LINQ

一曲冷凌霜 提交于 2019-11-28 02:28:16
Given the below DTO definitions: [Serializable] internal class OrderCollection : List<Order> { } [Serializable] internal class Order { public string OrderId { get; set; } public OrderDetailCollection OrderDetails { get; set; } } [Serializable] internal class OrderDetailCollection : List<OrderDetail> { } [Serializable] internal class OrderDetail { internal OrderDetail() { } /*public string ParentOrderId { get; set; }*/ public string ItemName { get; set; } public int Quantity { get; set; } } and the following xml: <root> <orders> <order orderId="ABC123"> <orderDetails> <orderDetail itemName=

Implementing RANK OVER SQL Clause in C# LINQ

我的未来我决定 提交于 2019-11-27 23:16:58
I need to implement the following T-SQL clause .... RANK() OVER (PARTITION BY a.CategoryKey ORDER BY (x.Rate * @BASE_RATE ) DESC )as Rank ...in C# LINQ. So far what I've come up with is something like .... var rank = data.GroupBy(d => d.CategoryKey) .Select(group => group.OrderByDescending(g => g.Rate * @BAES_RATE) I think this would give me each rank partition ordered by rate * BASE_RATE. But what I actually need is the individual rank of a single row, with this being a subquery within a larger result. So really the full SQL query I'm working from is something like .... SELECT a.Rate, a

How to PLINQ an existing LINQ query with Joins?

夙愿已清 提交于 2019-11-27 22:14:44
I'm using LINQ to compare two DataSets with each other to create new rows and update existing. I've noticed that the complete comparison lasts ~1,5 hours and only one of the two cores is busy(Task-Manager is 50-52% CPU Usage). I must admit that I'm completely new to parallel LINQ, but I assume that it could increase performance significantly. So my question is, how and what should I parallelize? These are the original queries(reduced to the essentials): 'check for new data Dim srcUnique = From row In src.Email_Total Select Ticket_ID = row.ticket_id, Interaction = row.interaction, ModifiedAt =

C# Ranking of objects, multiple criteria

旧街凉风 提交于 2019-11-27 20:17:16
I am building a plugin for a LAN party website that I wrote that would allow the use of a Round Robin tournament. All is going well, but I have some questions about the most efficient way to rank over two criteria. Basically, I would like the following ranking layout: Rank Wins TotalScore PersonE 1 5 50 PersonD 2 3.5 37 PersonA 2 3.5 37 PersonC 4 2.5 26 PersonB 5 2.5 24 PersonF 6 0 12 In SQL server, I would use: SELECT [Person], RANK() OVER (ORDER BY Wins DESC, TotalScore DESC) [Rank], [Wins], [TotalScore] Now, I only have List, Dictionary, and etc. to work with Specifically: Dictionary

(ID/ParentID) list to Hierarchical list

泪湿孤枕 提交于 2019-11-27 19:52:59
MyClass consists of ID ParentID and List<MyClass> as Children I have list of MyClass like this ID ParentID 1 0 2 7 3 1 4 5 5 1 6 2 7 1 8 6 9 0 10 9 Output (Hierarchical list) as List<MyClass> 1 __ 3 |__ 5__ 4 |__ 7__ 2__ 6__ 8 |__ 11 9 __10 What is the simplest way to achieve this in linq? P.S.: ParentID not sorted Edit: My try: class MyClass { public int ID; public int ParentID; public List<MyClass> Children = new List<MyClass>(); public MyClass(int id, int parent_id) { ID = id; ParentID = parent_id; } } initialize sample data and try to reach hierarchical data List<MyClass> items = new List

Linq OrderBy against specific values

荒凉一梦 提交于 2019-11-27 18:43:59
Is there a way in Linq to do an OrderBy against a set of values (strings in this case) without knowing the order of the values? Consider this data: A B A C B C D E And these variables: string firstPref, secondPref, thirdPref; When the values are set like so: firstPref = 'A'; secondPref = 'B'; thirdPref = 'C'; Is it possible to order the data like so: A A B B C C D E If you put your preferences into a list, it might become easier. List<String> data = new List<String> { "A","B","A","C","B","C","D","E" }; List<String> preferences = new List<String> { "A","B","C" }; IEnumerable<String> orderedData

How to debug a LINQ Statement

[亡魂溺海] 提交于 2019-11-27 18:29:08
I have a Linq to objects statement var confirm = from l in lines.Lines where (l.LineNumber == startline.LineNumber) || (l.LineNumber == endline.LineNumber) select l; The confirm object is returning an 'Object Null or Not A Reference' at at System.Linq.Enumerable.WhereListIterator`1.MoveNext() If the result of the query was empty, it would just return an empty enumerator. I know for a fact that there are no null objects in the statement. Is it possible to step through the LINQ statement to see where it is falling over? EDIT When I said I know for a fact that there are no null objects it turns

linq to entities vs linq to objects - are they the same?

南笙酒味 提交于 2019-11-27 18:22:37
I usually use the term entity to represent a business data object and in my mind, the linq to entities and linq to objects were the same. Is that not correct? That is definitely not the case. LINQ-to-Objects is a set of extension methods on IEnumerable<T> that allow you to perform in-memory query operations on arbitrary sequences of objects. The methods accept simple delegates when necessary. LINQ-to-Entities is a LINQ provider that has a set of extension methods on IQueryable<T> . The methods build up an expression tree (which is why delegates are actually passed as Expression<> s), and the