Hi Nick, Frank, and all,
Here is some of the X# samples I promised, along the lines that Nick suggested.
We will see some LINQ and Extension method syntax.
Checkout image _01 below :-
- PearsSum_01.jpg (32.07 KiB) Viewed 290 times
This is some simple code to add together (sum) each and every identity value of the Product objects in the collection. As you can see the full total is 105.
Now let us make a small sub-list of IDs - those of 3,5 and 9 :-
- PearsSum_02.jpg (52 KiB) Viewed 290 times
With the addition of the WHERE clause we can filter out from the whole set of identities, those included in the small sub set (or sub-list).
Note we are using 'Contains' and a similar approach exists in T-SQL script. The code is quite readable I feel.
So what is the sum of all those IDs that we have so far ignored, or filtered out ?
Well, here is one way to get an answer :-
- PearsSum_03.jpg (55.31 KiB) Viewed 290 times
In the code of the above image we are stating in the WHERE clause that the identity of the Product item in focus is NOT contained in our short list, or small set.
Now lets look at a convenient and very readable way of generating this 'other' set, all those IDs which are not 3,5, or 9. Check this out below :-
- PearsSum_04.jpg (70.13 KiB) Viewed 290 times
Line 149 shows some 'set algebra' in use, we often forget this very useful approach when dealing with collects, which after all are sets of objects. So lets think 'SET'.
One set 'taken' from another is an 'Except' in extension method use.
So now we can even do a double negative - see below for some 'fun' coding :-
- PearsSum_05.jpg (37.52 KiB) Viewed 290 times
Checkout line 154 !
Finally, we will look at adding sets together before we do our WHERE clause. Lets try some UNION work :-
- PearsSum_06.jpg (56.38 KiB) Viewed 290 times
And if we are then to use the joined up set as an ID check-list we can see this code at work :-
- PearsSum_07.jpg (89.06 KiB) Viewed 290 times
Okay then folks, lets hope that made some sense to some of you.
Note I have used the 'query' style of coding, but that used by Nick (extension method coding extensively) is still very similar in many ways. And we can't express all code in query syntax either, so we have to know how to use some EMs in our code.
Fingers crossed,
Phil.