Language Integrated Query (LINQ) is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages.

learn more… | top users | synonyms

6
votes
1answer
121 views

Reduce or improve Linq query with nested from-where clauses

I am new to linq to objects in C#. I have the following query which works adequately. Is there someway to reduce or rewrite this query using joins or something else? var query = from ...
5
votes
2answers
159 views

Optimising and error handling Linq query

I have 2 tables as below: Downtime DowntimeCode I am then doing this to get the Description field from the DowntimeCode table for the DowntimeCode with the sum highest value in the Downtime ...
1
vote
1answer
107 views

Making adding to a dictionary more efficient?

I have the following code and it's taking quite some time to run. I'm getting an Out of Memory Exception due to the high volume of answers there are. Is there any way I could speed it up? var ...
12
votes
3answers
247 views

Multiple indexes over an in memory collection for faster search

I have a big in-memory collection of following simplified class: public class Product { public int Id { get; set; } public string UserName { get; set; } public int CategoryId { get; set; ...
1
vote
1answer
57 views

A genric extension method to filter Linq-EF queries

I have various types of EF entities, all of them have a navigation property called "Employee". When generating reports the user will have the option to filter the report according to the different ...
8
votes
2answers
142 views

Should a year and month be stored as separate fields or as a date?

We have a table with calculated data that groups sales by product, year and month, to provide fast querying for statistics. My colleague argues that the year and month should be two separate fields, ...
4
votes
1answer
87 views

Convert looping statement to linq mode

I have a model object class like this public class EventInterestsResponse { public string id { get; set; } public int sortOrder { get; set; } public string name { get; set; } public string ...
0
votes
0answers
42 views

Can this DbGeography calculation be done better?

Given the following piece of code, can it be done better: public object DistanceFrom( IEnumerable<Coordinate> coordinates) { IEnumerable<DbGeography> addresses = ...
6
votes
2answers
97 views

Improving speed of moving average calculation

I am hoping to make this method run a little faster. I typically need to run this on a list with more than 100000 entries. Note that at the start and end of the list, I wish to weight the average so ...
1
vote
1answer
63 views

linq order by performance question

This is code is taking 10 seconds for just 64 items, just wondering if there is a faster way instead of using OrderBy in Linq (thinking about using Sort) myList = myList.OrderByDescending(x => ...
7
votes
1answer
122 views

NHibernate select with Query Over optimization for user roles base case

I have a complicated select procedure that I solved with a 5 query transaction, so I am posting it here to get suggestions on how I can shorten it up a bit. I am hoping that someone can help me make ...
5
votes
1answer
106 views

Refactoring chart generator in Linq

I'm kinda curious how I'd refactor this in the best way. I've only used Linq for simple queries. What it does: Depending on the type that is submitted, we're grouping events or payments by ...
16
votes
4answers
272 views

SudokuSharp Solver with advanced features

Even though it's the first time I'm writing something this "big", it feels like I know C# quite good. It's been nice to learn LINQ also and I am very impressed by the features, and perhaps I have ...
8
votes
1answer
139 views

Poker Hand Evaluator, take 2

This is following up on my previous attempt, which was admittedly done fast and not-so-well. This code attempts to allow comparing two poker hands to determine a winner, not only evaluating a given ...
2
votes
3answers
325 views

Convert Sql LIKE to Regex

I have a bit of code that converts a Sql Like expression to a regex expression for the purposes of a Linq to objects Like extension method. For some time I have been using this conversion. This ...
3
votes
2answers
136 views

Poker Hand Evaluator Challenge

This week's review challenge is a poker hand evaluator. I started by enumerating the possible hands: public enum PokerHands { Pair, TwoPair, ThreeOfKind, Straight, Flush, ...
5
votes
3answers
129 views

Proper and elegant way of Building a XML Document

I need to build the following XML document <?xml version="1.0"?> <SFDocument> <TableID>5415</TableID> <PageSize>1</PageSize> ...
5
votes
1answer
187 views

Is there a better, faster, more elegant way to create this dictionary?

I have a List of objects List<myObject> objectList and each object contains a List of strings like so: myObject: StringList( "A_1", "B_1", "C_1", "D_1", "E_1", "F_1" ) myObject: StringList( ...
9
votes
1answer
149 views

C++ linq-like library

I want to implement something like C# Linq in C++ Currently only select and where is implemented, but others should be fairly easy if I can make a good structure. The main concerns I have is I am ...
2
votes
1answer
133 views

Nested select Linq

Is there a simpler and optimized way to calculate listAdminCorePriveleges and privileges ? public string[] GetPrivilegesForUserByPersonEntityId(int personEntityId) { var listPrivileges = ...
3
votes
1answer
124 views

Improve speed of LINQ query

How can I improve the speed of this LINQ query? using (var txn = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadUncommitted ...
2
votes
1answer
108 views

Is there a way to eliminate the repetition in this linq query?

This is a query to return search results, but I feel it could be cleaned up a little. I just don't know how. var queryResult = (from r in dc.Retailers where ...
5
votes
4answers
340 views

How to optimize a multiple if-else with LINQ?

I have a static class: public static class ConfigurationDetails { public static string SharepointServer { get; set; } public static string Username { get; set; } public static string ...
1
vote
1answer
229 views

Improving MVC 4 DropDownList

I'm currently populating my drop downlists like this... public List<NewLogin> GetRolesForDDL() { using (database db = new database()) { return (from r in ...
2
votes
2answers
90 views

Add item to an Array 2D using LINQ

I've wrote a generic function to add an item to an Array 2D This is what I have: Private Sub Add_Item_Array_2D(ByRef Array_2D As String(,), _ ByVal Items As String()) ...
6
votes
2answers
124 views

Function for obtaining the 3 latest patient vital signs

I'm just putting together a small project, checking to see if this would be the fastest/cleanest way to write out a function that grabs the 3 latest vital signs. Edit: I am thinking ...
1
vote
0answers
789 views

Using LINQ ForEach instead of for/foreach (from a code quality PoV, not performance) [closed]

I was peer-reviewing some code recently and there was a particular thing that bothered me (although I could not tell why), regarding the use of LINQ expressions. Every single loop was written as a ...
2
votes
1answer
114 views

Check if Item exists in Grid using Linq

I have a grid which allows a user to enter a new Grid item. If the grid item exists or the tb is empty it displays the error. Here's the code I came up with. Can anybody think of a cleaner or more ...
1
vote
1answer
71 views

Refactor similar queries but reverse side of inequality operator

How can I refactor this? public static IQueryable<T_COMPANY> WhereTotalTransactionOrShowCaseAtLeast(this IQueryable<T_COMPANY> query, int minimum, DateTime? date = null) { if (date == ...
3
votes
2answers
153 views

Code duplication where impliment extension method for IEnumerable and IQueryable on entity set

Since Entity Set does not support asqueryable, I have to do this. How can I reduce the amount of redundant code between the two methods? public static IQueryable<T> ...
3
votes
1answer
154 views

Filtering a collection by an async result

I'm trying to do something like this: var loudDogs = dogs.Where(async d => await d.IsYappyAsync); The "IsYappyAsync" property would return a Task<bool>. Obviously this isn't supported, so ...
2
votes
1answer
169 views

Calculating GetHashCode efficiently with unordered list

I'm wondering what would be the best way to calculate the hashcode when the order of a sequence doesn't matter. Here's the custom IEqualityComparer<T> i've implemented for an answer on ...
3
votes
2answers
92 views

How can this LINQ query be improved?

Here's the query: using (var db = CreateContext()) { // performing the check for HasBeenAdded inline here, is this only one db call? return db.Subjects.Select(s ...
2
votes
1answer
720 views

Can this LINQ lambda expression be written any better?

For the sake of knowledge, I wanted to convert a SQL query to a LINQ query expression. Here is the original SQL: SELECT CT.COURSE_NODE_ID AS CNID, CT.NODE_TEXT FROM COURSE_RELATED_VERSIONS AS CRV ...
4
votes
1answer
213 views

Null replacement - Is this LINQ readable?

This is the original version of code written by my coworker that replaces every null cells with an empty string. for (int i = 0; i < dGV_factory.Rows.Count; i++) { ...
3
votes
4answers
412 views

How to optimize these nested loops for better performance

I need to optimize this code so it can execute faster, even if that means using more memory for (int t = 0; t < Clientes[0].ChildNodes.Count; t++) { for (int i = 0; i ...
4
votes
2answers
107 views

Optimizing join statement

I have this piece of code: List<myItem> items = (from x in db.myItems join y in db.tblParents on x.item_parent equals y.id where !y.hidden ...
2
votes
1answer
6k views

Faster way to convert DataTable to List of Class

I am using ExcelDataReader to import an excel file to a dataset. Example Excel table below: //ID Name Display Order Active //1 John 1 1 ID, DisplayOrder and ...
3
votes
2answers
139 views

Parallel LINQ Performance [closed]

This a sample code, when it runs it take only about 46% CPU time I'm using code based on this sample to process big TSV files (from 100GB to 500GB) I tried to use BlockingCollection but performance ...
3
votes
1answer
238 views

Inline conversion/cast from Expression<Func<T, int>> to LambdaExpression

I have the following code (I need the LambdaExpression as not all Func<>'s are T, int): var orderDelegates = new Dictionary<string, LambdaExpression>(); Expression<Func<Image, ...
2
votes
2answers
264 views

Change one column data from List<>

I have this code: for (int i = 0; i < lst.Count(); i++) { lst[i].ColumnOrder = (short)i; } But I was trying to find a way to do it via LINQ or something else with better performance. I'm ...
1
vote
0answers
68 views

Calendar challenge more LINQ [closed]

I am challenging myself to do this calendar problem but using C#/linq. Here is a demo of my output I think the temp weeks can be removed. I just don't know how to say for each month give me 6 row ...
2
votes
2answers
128 views

Reducing memory footprint in anagram-finding program for text

public static bool IsAnagramOf(this string word1, string word2) { return word1.OrderBy(x => x).SequenceEqual(word2.OrderBy(x => x)); } public static void Main() { Console.SetIn(new ...
5
votes
1answer
130 views

Can this ordering logic be done better?

I am creating an ASP.NET MVC 4 app using Entity Framework and currently I am creating orderable tables. For that I have created the following logic to handle the different orders: if (orderKey == ...
2
votes
3answers
237 views

Write nested loops as linq query

I am tracking trains and trying to identify individual trains seen multiple times at different points through the IDs of the wagons on them when spotted. // create a lookup of all tracked trains for ...
-1
votes
2answers
130 views

How does this GetType() work? [closed]

I thought .GetType() was a method that had to be used like myProperty.GetType(). But, as you can see, it is being used simply by itself: (from p in GetType().GetProperties(BindingFlags.Instance | ...
0
votes
3answers
60 views

complex linq query to improve?

I have written the following Query to retrieve some object's ID. if there are objects in the list that have a larger numbinbatch than saveditems number in batch then it should take that. if not then ...
3
votes
1answer
275 views

Transforming http request into linq query

I wrote something that has probably done thousands of times: a function that takes a parsed http query as input and return a linq query. Any input is appreciated. public IList<Lead> ...
1
vote
2answers
78 views

Refactor LINQ & XML code

I have some duplicate code using LINQ & XML. I'm sure there is a way to refactor it, but I'm not sure how to go about doing it. Would someone help, please? var fileInfo = ...
2
votes
1answer
124 views

Refactor Linq Expression

Is there any way I can refactor this expression tree? public static IOrderedQueryable<T_PO> SortedPO(this IQueryable<T_PO> query, ExtjsGridPagingParameter.SortingParameter ...