score:6

Accepted answer

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

enumerable.groupby method

groups the elements of a sequence.

enumerable.select method

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.


Related Query