Language Integrated Query (LINQ) is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages.
4
votes
4answers
117 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
22 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 ...
1
vote
0answers
11 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())
...
4
votes
2answers
101 views
Need a review for a function that returns the 3 latest dates
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 GetRecentVitalSigns ...
1
vote
0answers
75 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
34 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
64 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 == ...
2
votes
2answers
91 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> ...
2
votes
1answer
81 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
0answers
65 views
Selecting according to a Min or Max on one value in LINQ [migrated]
This is equivalent to a correlated subquery, where you want the row which has the biggest or smallest X (and not the biggest or smallest X itself). I've only been able to get it through sorting and ...
2
votes
1answer
85 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 ...
1
vote
0answers
29 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
135 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
102 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
173 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
97 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
...
1
vote
1answer
778 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
102 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
90 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
89 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
50 views
Calendar challenge more LINQ
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
108 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
112 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
110 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
71 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
52 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
171 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
1answer
61 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 = ...
1
vote
1answer
66 views
Refactor LINQ & XML code [duplicate]
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
103 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 ...
1
vote
1answer
27 views
Should I use and loop, or just a loop?
I build a code like this
if(listObj.Any(x => x.id < 0))
{
foreach(ModelClass item in listObj)
{
if(item.id < 0)
{
// code to create a new Obj in the ...
2
votes
1answer
189 views
I've started Tuples to LINQ and I'm not sure if it's an anti pattern
I have
public static class TupleExtensions {
public static IEnumerable<T> SelectMany<T>(this IEnumerable<Tuple<T, T>> te)
{
foreach (var t in te)
{
...
2
votes
1answer
102 views
How can I make this code better
I hate having almost two identical blocks do almost the exact same thing.
I am working with Entity Framework 4.4.0.0
Asp.Net MVC
Right now if the user is a "manager" I want to show them the entire ...
4
votes
1answer
62 views
More effecient way of running this LINQ
So, what the function is supposed to do is count the number of times each letter appears in a string, then display the one that appears the most, and the number of times it appears. If there's a tie, ...
1
vote
1answer
77 views
Optimize Implementation - Wait for all threads using LINQ
In my program I read several information's about the system. I read each categories like CPU, memory, services, software and so on with a own thread. The main program have to wait for the longest ...
3
votes
3answers
256 views
Linq query performance improvements
As I am getting my Linq query to a functional point, I start looking at the query and think about all the "ANY" , and wonder if those should be a different method and then I have data conversions ...
6
votes
2answers
257 views
Improvement requested for: Generic Calculator and Generic Number
.NET does not support generic numbers. It is not possible to enforce a generic method with generic argument T that T is a number. The following code will simply not compile:
public T ...
2
votes
3answers
923 views
Using Linq to select the first and last values
I want to get just the first and last values in a date range. I have the following code:
using (var myEntities = new dataEntities())
{
var myValues = (from values in myEntities.PointValues
...
2
votes
1answer
150 views
XML to Windows.Forms.Keys List
It took me a lot of poking around and unit testing to get the code to look like it is right now. So I have a XML file which in part looks like this
<FunctionKeys>
...
4
votes
3answers
173 views
LINQ Group query - simplifying
Is there a way of writing this correctly working query more efficiently? I'm asking only to learn LINQ better:
var cpuInfo = edgeLPs
.GroupBy(k => k.CPU.ID, e => e.CPU)
...
2
votes
1answer
134 views
Is there a more elegant way to rewrite this piece of code?
This is what happens, i have a dropdownbox "ActiviteitAardItems" where ActiviteitAard items can be checked (checkbox). If one (or more) are checked the property Opacity will be changed, and the code ...
3
votes
1answer
99 views
LINQ statement to aggregate data
Teachers can raise 'dramas' which are when students forget equipment, don't do homework or are flagged for concern. Each drama for each student is stored as a separate record.
I seek a list of all ...
1
vote
1answer
242 views
MVC4 Linq to Sql Model Structure
Goal: To pass a list of related objects into a view which can then be iterated over using razor syntax.
I'm new to LINQ and MVC4. I'm trying to use the repository pattern to manage my model creation. ...
6
votes
2answers
164 views
Left hand search in string
the requirement for our search query is to look for all search words in the beginning of vehicle manufacturer, model and variant name. Case insensitive. The word being a string of characters separated ...
0
votes
1answer
95 views
making this linq query faster
I need the most efficient way to get the value of the searchName, or if null return searchName. I am returning just a single string, so maybe I should search itemRepository.Item instead of Items
...
3
votes
2answers
109 views
Refactoring from .. in .. select to more compact one
Is it possible to write the two following lines of code in a more compact one?
IEnumerable<int> collection = from partnerNumbers in contract.Contact.PartnerNumbers select partnerNumbers.Pnr;
...
3
votes
3answers
258 views
Rewrite from .. where .. select to more compact one
This is the code:
public DateTime GibSomeStartDate(IEnumerable<int> partnerNumbers, DateTime startTime)
{
return (from contract in this.databaseContext.Contract
where ...
3
votes
4answers
126 views
Finding unconnected sets
I have a list of boxes, and wish to group them into unconnected sets of overlapping boxes. (Note that two boxes A and B may not overlap each other, but if they are both overlapped by a box C, they ...
0
votes
3answers
169 views
New to LINQ, not sure this is best practice
So i have a super class that has a 3 child classes. Those 3 child classes have multiple classes of their own. I was having troubles saving this into XML, so I decided to parse it out and do a little ...
1
vote
1answer
41 views
Linq to sql performance
I'm creating a system and I'm using EF with Linq. Model First.
So I create my Model (.edmx) and using it, I generated my database and my classes. like the Usuario (user in portugues. going to keep ...