score:6
use a groupby
(preferably at the db iqueryable
level), then for extra flavor you can to todictionary
to make the look up more efficient
var dict = lst.groupby(x => x.statusid)
.select(x => new { x.key, count = x.count() })
.todictionary(x => (projectstatus)x.key, x => x.count.tostring());
lblactive.innertext = dict[projectstatus.project_active];
lblcompleted.innertext = dict[projectstatus.project_completed];
...
the assumption is the statusid
maps one to one with projectstatus
note the additional select
, is superfluous however is added for in-case you just wanted a list
additional resources
groups the elements of a sequence.
projects each element of a sequence into a new form.
enumerable.todictionary method
creates a dictionary from an ienumerable.
score:1
you could easily use tolookup
like this:
list<rfpmodel> lst = new list<rfpmodel>();
lst.add(new rfpmodel { statusid = 1 });
lst.add(new rfpmodel { statusid = 1 });
lst.add(new rfpmodel { statusid = 1 });
lst.add(new rfpmodel { statusid = 2 });
lst.add(new rfpmodel { statusid = 2 });
lst.add(new rfpmodel { statusid = 3 });
lst.add(new rfpmodel { statusid = 4 });
lst.add(new rfpmodel { statusid = 4 });
lst.add(new rfpmodel { statusid = 4 });
lst.add(new rfpmodel { statusid = 4 });
lst.add(new rfpmodel { statusid = 5 });
var lookup = lst.tolookup(p => p.statusid);
var status1count = lookup[1].count(); // 3
var status2count = lookup[2].count(); // 2
var status3count = lookup[3].count(); // 1
var status4count = lookup[4].count(); // 4
var status5count = lookup[5].count(); // 1
replace the x in the lookup[x] part with the appropriate enums.
score:1
lookup would be a good solution to have.
list<rfpmodel> lst = rfpmodel.getall(); //all
var lookup = lst.tolookup(x=>x.statusid);
lblactive.innertext = lookup[(int)projectstatus.project_active].count();
lblcompleted.innertext = lookup[(int)projectstatus.project_completed].count();
and so on ......
you can read more on lookup here
score:2
you can do something like this, only looping through the list once (here i only show 4 counts. you can easily add the rest yourself):
int activecount = 0;
int completedcount = 0;
int proposalcount = 0;
int proposalrejcount = 0;
foreach (var item in lst) {
if (item.statusid == (int)projectstatus.project_active)
activecount++;
else if (item.statusid == (int)projectstatus.project_completed)
completedcount++;
else if (item.statusid == (int)projectstatus.project_underprocess)
proposalcount++;
else if (item.statusid == (int)projectstatus.project_proposal_rejected)
proposalrejcount++;
}
lblactive.innertext = activecount.tostring();
lblcompleted.innertext = completedcount.tostring();
lblproposal.innertext = proposalcount.tostring();
lblproposalrej.innertext = proposalrejcount.tostring();
but only do this if this particular code is actually causing a performance problem. don't optimise prematurely.
Source: stackoverflow.com
Related Query
- Accessing a List multiple times in same code block - Any better approach?
- comparing multiple items in the same list
- How to find how many times the same object appears in a list and then find a property of the most appeared object
- LINQ: single list which consists of multiple properties of same type of object collection
- Does Entity Framework query the database multiple times if I use different fields of the same Linq query at different times?
- Use LINQ to concatenate multiple rows list with same value into single row
- Select only the values present multiple times in a list C# Linq
- LINQ: A better way to grouping a list of objects by multiple properties in to a list of lists?
- Linq query is triggered multiple times without any apparent reason
- Assigning anonymous type multiple times with the same property names and types
- select from list which has same code
- Any better way of accessing values from database-LinqToSql
- How to improve performance of ordering list multiple times after getting data from database
- How to sort list on multiple properties in one line of code in Linq
- C# LINQ Find List Inside Another List, Better way to code this than a foreach loop
- Class with multiple List Properties of same type but with different restrictions
- Getting Sum of Multiple Columns and summing up values for the same id from a list
- LINQ to SQL querying same column multiple times
- C# linq: Check multiple conditions on a list on the same property field
- Better way to write this C# Code For List of Lists
- Rename List item when there is the same string multiple time
- Is it better to store query results in memory, or run the same query multiple times?
- List or Array of String Contain specific word in Html Source Code
- Why is my code returning a list of the same object 4 times?
- Sort multiple list inside List of objects with the same order of the first list
- EF core eager loading - loads same data multiple times
- Is there any better way to check if the same data is present in a table in .Net core 3.1?
- C# XElement is not getting multiple Values for the same Tag multiple times
- c# Linq or code to extract groups from a single list of source data
- Multiple same item inside list
More Query from same tag
- Sum nested values with Linq
- use LINQ to XML to Programmatically get all differences between 2 xml files
- Linq with join, group and sum not working
- How to access the member of an IGrouping?
- How to modify this command to pull multiple results from the database
- LINQTOSql Missing parameter question
- How to use Group by of two elements in an object and get the count in List?
- LINQ with sub select query using sum and group by?
- Obtain max length for a "string" column using LINQ to SQL
- Display only Allow Values for User in Linq query
- Linq update many-many relationship
- orderby with LINQ
- Entity Framework 4: SQL to Linq: How to select strings that are not uppercase?
- Generate True False boolean enumerable in LINQ
- LINQ: large list of strings
- How do i convert from one collection to another
- How to cancel asynchronuos action in LINQ query?
- groupby count and percentage
- Select the most recent date in a LINQ query with an inner join
- converting from linq var result to another object
- What is the correct way to Take() X-number of records with LINQ but find out if there are more
- Linq to XML + chaining expressions
- {} in linq result
- SQL to LINQ Left Outer Join with Multiple Conditions
- Get properties of a type in a list in a dictionary
- LINQ query to calculate values from joined collection
- VB.net & LINQ - identifying date types
- How to achieve LINQ SUM with multiple columns with no group by
- Why would you create a label target of type void with no name?
- Can I do this with LINQ?