how to use count on a collection

T

Tony Johansson

Hello!

I have this simple linq query.
var totalRows = from o in ccrFormObj.myWorksheetRowList
where o.WorksheetID == workSheetID
select o;

In this query I'm only interested in the number of match I get.
So to get every record is of no interest to me.
So in a way using pseducode I want to get something like
var totalRows = from o in ccrFormObj.myWorksheetRowList
where o.WorksheetID == workSheetID
select o.Count()


//Tony
 
A

Anthony Jones

Tony Johansson said:
Hello!

I have this simple linq query.
var totalRows = from o in ccrFormObj.myWorksheetRowList
where o.WorksheetID == workSheetID
select o;

In this query I'm only interested in the number of match I get.
So to get every record is of no interest to me.
So in a way using pseducode I want to get something like
var totalRows = from o in ccrFormObj.myWorksheetRowList
where o.WorksheetID == workSheetID
select o.Count()

Linq isn't allways the answer. If all you want is the count of items that
meet a simple criteria then:-

int totalRows = ccrFromObj.myWorksheetRowList.Count(o => o.WorksheetID ==
workSheetID);
 
T

Tony Johansson

Hello!

This was nice. It was a simple way to get the number where I have condition.

Can you just explain how this work.
int totalRows = ccrFromObj.myWorksheetRowList.Count(o => o.WorksheetID ==
workSheetID);

If I want to learn more about this kind of feature what is it called if I
want to search for more information about this.

//Tony
 
A

Anthony Jones

Tony Johansson said:
Hello!

This was nice. It was a simple way to get the number where I have
condition.

Can you just explain how this work.
int totalRows = ccrFromObj.myWorksheetRowList.Count(o => o.WorksheetID ==
workSheetID);

myWorksheetRowList will be an implemenation of IEnumerable<T>
Where in this case T is a WorksheetRow class.

..Count<T> is an extension method of IEnumerable<T> which as an overload that
takes a predicate used to limit the items to be counted, only those that
match the predicate will be counted. The predicate is a delegate type
Func<T, bool>.

o => o.WorksheetID == workSheetID

is known as a Lambda expression. In this case it results in an anonymous
function which if we were using C# 2 we would have to code like this:-

delegate(WorksheetRow o) { return o.WorksheetID == workSheetID; }

Note that Func<T, bool> defines a delegate that takes a single parameter of
type T (which we established ealier is WorksheetRow in this case) and
returns a boolean. The anonymous function above conforms to that definition
and hence can be passed to the Count method.

So count enumerates all the items passing each item to the anonymous
function, it only increments the count of items if the anoymous function
returns true.
If I want to learn more about this kind of feature what is it called if I
want to search for more information about this.

Look up Lambda C#, extension methods and IEnumerable<T>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top