linq-to-objects

Linq Except with custom IEqualityComparer

删除回忆录丶 提交于 2019-11-27 12:03:30
问题 I am trying to find the difference between two generic lists, as in the example below. Even though t1 and t2 contain the same properties, they are not the same object, so I have need to implement an IEqualityComparer. This appears to be working with this example, but the real class has several other properties and I also need to do the same with a few other class. So I was wondering if I am re-inventing the wheel? Is there an easier method of comparing all the properties of two objects? At

IList<T> to IQueryable<T>

狂风中的少年 提交于 2019-11-27 08:32:44
I have an List and I'd like to wrap it into an IQueryable. Is this possible? Paul van Brenk List<int> list = new List<int>() { 1, 2, 3, 4, }; IQueryable<int> query = list.AsQueryable(); If you don't see the AsQueryable() method, add a using statement for System.Linq . Chris Shaffer Use the AsQueryable<T>() extension method. 来源: https://stackoverflow.com/questions/73542/ilistt-to-iqueryablet

Splitting an array using LINQ

拜拜、爱过 提交于 2019-11-27 06:12:03
问题 I have a collection uni-dimensional like this: [1,2,4,5.....n] I would like to convert that collection in a bi-dimensional collection like this: [[1,2,3], [4,5,6], ...] Basically I want to group or split if you want, the array in groups of 'n' members I can do it with a foreach statement, but I am currently learning LINQ so instead of iterating through all elements and create a new array manually I would like to use the LINQ features (if applicable) Is there any LINQ function to help me to

How to PLINQ an existing LINQ query with Joins?

我们两清 提交于 2019-11-27 04:33:54
问题 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 =

“Nested foreach” vs “lambda/linq query” performance(LINQ-to-Objects) [closed]

筅森魡賤 提交于 2019-11-27 04:02:17
In performance point of view what should you use "Nested foreach's" or "lambda/linq queries"? Write the clearest code you can, and then benchmark and profile to discover any performance problems. If you do have performance problems, you can experiment with different code to work out whether it's faster or not (measuring all the time with as realistic data as possible) and then make a judgement call as to whether the improvement in performance is worth the readability hit. A direct foreach approach will be faster than LINQ in many cases. For example, consider: var query = from element in list

Linq to Objects - return pairs of numbers from list of numbers

爷,独闯天下 提交于 2019-11-27 02:09:05
var nums = new[]{ 1, 2, 3, 4, 5, 6, 7}; var pairs = /* some linq magic here*/ ; => pairs = { {1, 2}, {3, 4}, {5, 6}, {7, 0} } The elements of pairs should be either two-element lists, or instances of some anonymous class with two fields, something like new {First = 1, Second = 2} . None of the default linq methods can do this lazily and with a single scan. Zipping the sequence with itself does 2 scans and grouping is not entirely lazy. Your best bet is to implement it directly: public static IEnumerable<T[]> Partition<T>(this IEnumerable<T> sequence, int partitionSize) { Contract.Requires

Implementing RANK OVER SQL Clause in C# LINQ

房东的猫 提交于 2019-11-26 23:17:32
问题 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

(ID/ParentID) list to Hierarchical list

痞子三分冷 提交于 2019-11-26 22:52:12
问题 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 =

C# Linq where clause as a variable

隐身守侯 提交于 2019-11-26 22:47:14
问题 I am trying to make a LINQ statement where the where clause comes from a variable. For example: string whereClause = address.zip == 23456; var x = from something in someList where whereClause; Is this possible? I cannot seem to get it to work. thanks, Update - my where clause is predefined and will be based on user input so I don't think this will work for me. Basically whereClause is not constructed in the method, it is a parameter of the method which does the LINQ. I didn't explain that

How to debug a LINQ Statement

浪子不回头ぞ 提交于 2019-11-26 22:41:22
问题 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