You should probably assign the Member.GetAll to a local variable before doing the lambda expression. I'm not quite sure how the GetEnumerator() method is evaluated by the runtime engine but the GetAll accessor executes a database query. For example these two statements hit the database which returns every record each time:
var first = Member.GetAll[0]; var count = Member.GetAll.Length;
If you have 100 members all 100 members are returned each time, regardless of what you want.
But if I was to do this:
var members = Members.GetAll; var first = members[0]; var count = members.Length
We only do a single DB call (yes, to return all 100 members though) rather than two.
Count of Members?
How do I do a count of members, other than by retrieving all of them and examining the length of the array?
Also, is it possible to do filtered counts (i.e., count of members with a property value == something)?
- Mark
You should probably assign the Member.GetAll to a local variable before doing the lambda expression. I'm not quite sure how the GetEnumerator() method is evaluated by the runtime engine but the GetAll accessor executes a database query. For example these two statements hit the database which returns every record each time:
If you have 100 members all 100 members are returned each time, regardless of what you want.
But if I was to do this:
We only do a single DB call (yes, to return all 100 members though) rather than two.
Interesting, thanks. Right now I'm executing a scalar sqlserver function that I put into the database:
ALTER FUNCTION [dbo].[sfNumberOfMembers]
(
)
RETURNS int
AS
BEGIN
DECLARE @retVal int = (SELECT COUNT(nodeId) FROM cmsMember)
RETURN @retVal
END
I'm pretty sure this is valid, although it's undesirable to be tied that closely to the table structure in case it changes.
- Mark
is working on a reply...