/
LINQExcept.razor
246 lines (198 loc) · 13.2 KB
/
LINQExcept.razor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
@page "/blogs/using-linq-except-to-find-difference-in-data"
@using LINQDemoComponents
@inherits FragmentNavigationBase
@inject TableOfContents tableOfContents
<Content Description=@Description
Slug=@Slug
PosterPath=@PosterPath
Channel="@Channel"
ContentType="@ContentType"
TotalContents=@TotalContents
Type="LINQ"
FileName=@nameof(LINQExcept)>
<ContentBody>
<p>
In this article, let's learn about how to use <ContentHighlight>Except</ContentHighlight> in <ContentHighlight>LINQ</ContentHighlight> in .NET.
</p>
<p>
<strong>Note:</strong> If you have not done so already, I recommend you read the article on
<NavLink class="[ underline ]" href="blogs/using-linq-sequence-equal-to-find-equality-of-data" Match="NavLinkMatch.All">Using LINQ Sequence Equal to Find Equality</NavLink>.
</p>
<h3 class="[ font-semibold text-lg ]">Table of Contents</h3>
<ol class="[ list-decimal ] [ ml-4 ]">
<li>
<NavLink class="[ underline ]" href="@($"blogs/{Slug}#introduction")" Match="NavLinkMatch.All">
Introduction
</NavLink>
</li>
<li>
<NavLink class="[ underline ]" href="@($"blogs/{Slug}#primitive-types")" Match="NavLinkMatch.All">
Using LINQ Except to compare primitive types
</NavLink>
</li>
<li>
<NavLink class="[ underline ]" href="@($"blogs/{Slug}#objects")" Match="NavLinkMatch.All">
Using LINQ Except to compare objects
</NavLink>
</li>
<li>
<NavLink class="[ underline ]" href="@($"blogs/{Slug}#equality-comparer")" Match="NavLinkMatch.All">
Using LINQ Except to compare with Equality Comparer
</NavLink>
</li>
<li>
<NavLink class="[ underline ]" href="@($"blogs/{Slug}#except-by-primitive-types")" Match="NavLinkMatch.All">
Using LINQ Except By to compare objects with primitive types
</NavLink>
</li>
<li>
<NavLink class="[ underline ]" href="@($"blogs/{Slug}#except-by-objects")" Match="NavLinkMatch.All">
Using LINQ Except By to compare objects
</NavLink>
</li>
<li>
<NavLink class="[ underline ]" href="@($"blogs/{Slug}#summary")" Match="NavLinkMatch.All">
Summary
</NavLink>
</li>
</ol>
<h3 id="introduction" class="[ font-semibold text-lg ]">Introduction</h3>
<p>
When working with two collections, we can find the difference between them using LINQ <ContentHighlight>Except()</ContentHighlight> method. The idea here is to find out
values in one which is not in other and returns the list of exceptions. With <ContentHighlight>Except()</ContentHighlight>, we can compare
two collections for Difference.
</p>
<p>
LINQ <ContentHighlight>Except()</ContentHighlight> is used to answer questions about collection such as
</p>
<ul class="[ list-disc ] [ ml-4 ]">
<li>Find phones that do not have sales</li>
<li>Find customers who have not ordered specific product</li>
<li>Compare data in tables between different environments</li>
</ul>
<h3 id="primitive-types" class="[ font-semibold text-lg ]">Using LINQ Except to compare primitive types</h3>
<p>
Primitive data types like <ContentHighlight>int</ContentHighlight>, <ContentHighlight>decimal</ContentHighlight>, <ContentHighlight>string</ContentHighlight>, etc can just
compare the values against other value in the collection
</p>
<GithubGistSnippet Title="LINQ Except Primitive Types" UserId="fingers10" FileName="8b857b012416dcce3c4507e6b6e62cec"></GithubGistSnippet>
<DemoSnippet Title="LINQ Except Clause Demo">
<p>
Let's try LINQ Except with integer type
</p>
<ol class="[ list-decimal ] [ ml-4 ]">
<li>Enter number and add it to respective sequence</li>
<li>For demo purpose I have restricted sequence length to 3</li>
<li>Click on Except Button to view the result</li>
<li>Click on reset to try other combination</li>
</ol>
<ExceptDemo></ExceptDemo>
</DemoSnippet>
<GoogleAdSense Type="GoogleAdSenseAdType.InArticle" Format="GoogleAdSenseAdFormat.Fluid" Style="text-align:center;" Slot="3914293965"></GoogleAdSense>
<h3 id="objects" class="[ font-semibold text-lg ]">Using LINQ Except to compare objects</h3>
<p>
Except checks for reference equality of two objects for <ContentHighlight>object</ContentHighlight> data types.
</p>
<GithubGistSnippet Title="LINQ Except Objects" UserId="fingers10" FileName="988e1a1b7d5a1cbd41aff90e32235b1d"></GithubGistSnippet>
<DemoSnippet Title="LINQ Except Clause Objects Demo">
<p>
Let's try LINQ Except with objects
</p>
<ul class="[ list-disc ] [ ml-4 ]">
<li>We have <ContentHighlight>Product</ContentHighlight> class with following properties - <ContentHighlight>Id, Name</ContentHighlight></li>
<li>We have <ContentHighlight>Sale</ContentHighlight> class with following properties - <ContentHighlight>ProductId, Price</ContentHighlight></li>
<li>The tables are loaded with product and sale collection.</li>
<li>Click on Except Button to view the result.</li>
<li>Click on reset to try other combination</li>
</ul>
<ExceptObjectDemo></ExceptObjectDemo>
</DemoSnippet>
<GoogleAdSense Type="GoogleAdSenseAdType.InArticle" Format="GoogleAdSenseAdFormat.Fluid" Style="text-align:center;" Slot="3914293965"></GoogleAdSense>
<h3 id="equality-comparer" class="[ font-semibold text-lg ]">Using LINQ Except to compare with Equality Comparer</h3>
<p>
So, equality for primitive data types with <ContentHighlight>Except()</ContentHighlight> is easy and straight forward, but with objects by default it's going to work by
comparing object references. But in most cases we want to make comparison based on one or more properties in the object. To do that we need to start by creating
<ContentHighlight>EqualityComparer<T></ContentHighlight> class.
</p>
<ol class="[ list-decimal ] [ ml-4 ]">
<li>Create a <ContentHighlight>ProductComparer</ContentHighlight> class that inherits from <ContentHighlight>EqualityComparer<Product></ContentHighlight> class.</li>
<li>Override <ContentHighlight>Equals(Product 1, Product 2) method.</ContentHighlight></li>
<li>Write the conditions to check equality and return true if both matches.</li>
<li>Also override <ContentHighlight>GetHashCode()</ContentHighlight> method and return unique value for every single object.</li>
</ol>
<GithubGistSnippet Title="LINQ Except Product Comparer" UserId="fingers10" FileName="6f0ec5f1ab8df9032e72b666daa372db"></GithubGistSnippet>
<GithubGistSnippet Title="LINQ Except With Product Comparer" UserId="fingers10" FileName="8a9859514f3d4e9f3c908bdd627aa9ce"></GithubGistSnippet>
<DemoSnippet Title="LINQ Except Clause with Comparer Demo">
<p>
Let's try LINQ Except with Comparer
</p>
<ul class="[ list-disc ] [ ml-4 ]">
<li>We have <ContentHighlight>Product</ContentHighlight> class with following properties - <ContentHighlight>Id, Name</ContentHighlight></li>
<li>The tables are loaded with two product collection.</li>
<li>Click on Except Button to view the result.</li>
<li>Unlike previous demo, here we use <ContentHighlight>ProductComparer</ContentHighlight> to compare objects based on their property values. So they will be equal irrespective of reference.</li>
<li>Click on reset to try other combination</li>
</ul>
<ExceptWithComparerDemo></ExceptWithComparerDemo>
</DemoSnippet>
<GoogleAdSense Type="GoogleAdSenseAdType.InArticle" Format="GoogleAdSenseAdFormat.Fluid" Style="text-align:center;" Slot="3914293965"></GoogleAdSense>
<h3 id="except-by-primitive-types" class="[ font-semibold text-lg ]">Using LINQ Except By to compare objects with Primitive Types</h3>
<p>
Starting .NET 6, Microsoft added <ContentHighlight>ExceptBy()</ContentHighlight> which is similar to <ContentHighlight>Except</ContentHighlight> in functionality but with an
simplification that we don't need to use a comparer class. We can use <ContentHighlight>Key Expression</ContentHighlight> to find the difference.
</p>
<GithubGistSnippet Title="LINQ Except By Primitive Types" UserId="fingers10" FileName="542c7f89995b986c23c3ff9fc5e0fd7a"></GithubGistSnippet>
<DemoSnippet Title="LINQ Except By Clause Demo">
<p>
Let's try LINQ Except By with integer type
</p>
<ol class="[ list-decimal ] [ ml-4 ]">
<li>Enter number and add it to sequence</li>
<li>For demo purpose I have restricted sequence length to 3</li>
<li>The Key Expression here is the Product ID. So whatever ID you enter in above sequence, ExceptBy() will compare and return the remaining Products from collection.</li>
<li>For demo purpose I will be displaying only Product ID's</li>
<li>Click on Except By Button to view the result</li>
<li>Click on reset to try other combination</li>
</ol>
<ExceptByDemo></ExceptByDemo>
</DemoSnippet>
<GoogleAdSense Type="GoogleAdSenseAdType.InArticle" Format="GoogleAdSenseAdFormat.Fluid" Style="text-align:center;" Slot="3914293965"></GoogleAdSense>
<h3 id="except-by-objects" class="[ font-semibold text-lg ]">Using LINQ Except By to compare objects</h3>
<p>
Previously, we saw a demo on how to find products that are not sold. We can also perform the same function using <ContentHighlight>ExceptBy()</ContentHighlight>. Unlike
<ContentHighlight>Except()</ContentHighlight>, <ContentHighlight>ExceptBy()</ContentHighlight> will return list of <ContentHighlight>Product</ContentHighlight> instead of
product Id's.
</p>
<GithubGistSnippet Title="LINQ Except By Objects" UserId="fingers10" FileName="402c5360837afa0c93b32b58806b4225"></GithubGistSnippet>
<DemoSnippet Title="LINQ Except By Clause Demo">
<p>
Let's try LINQ Except By with objects
</p>
<ul class="[ list-disc ] [ ml-4 ]">
<li>We have <ContentHighlight>Product</ContentHighlight> class with following properties - <ContentHighlight>Id, Name</ContentHighlight></li>
<li>We have <ContentHighlight>Sale</ContentHighlight> class with following properties - <ContentHighlight>ProductId, Price</ContentHighlight></li>
<li>The tables are loaded with product and sale collection.</li>
<li>The Key Expression here is the Product ID. So whatever ID you enter in above sequence, ExceptBy() will compare and return the remaining Products from collection.</li>
<li>For demo purpose I will be displaying only Product ID's</li>
<li>Click on Except By Button to view the result.</li>
<li>Click on reset to try other combination</li>
</ul>
<ExceptByObjectDemo></ExceptByObjectDemo>
</DemoSnippet>
<GoogleAdSense Type="GoogleAdSenseAdType.InArticle" Format="GoogleAdSenseAdFormat.Fluid" Style="text-align:center;" Slot="3914293965"></GoogleAdSense>
<h3 id="summary" class="[ font-semibold text-lg ]">Summary</h3>
<p>
In this article we learn't how to check for difference in data between collections using <ContentHighlight>Except</ContentHighlight> and <ContentHighlight>ExceptBy</ContentHighlight>.
This can be used to find if item is between collection matches or not and also we can compare object item using Comparer. All these can be used with any
<ContentHighlight>IEnumerable</ContentHighlight> or <ContentHighlight>IQueryable</ContentHighlight> types.
</p>
</ContentBody>
</Content>
@code {
private string Description = "In this post I will teach you how to find difference between collections using LINQ in .NET. All with live working demo.";
private string Slug = "using-linq-except-to-find-difference-in-data";
private string PosterPath = "Blogs/LINQ";
private string Channel = "linq";
private string ContentType = "blogs";
private ushort TotalContents => (ushort)tableOfContents.Contents.Count(content => content.Type.Equals("LINQ", StringComparison.CurrentCultureIgnoreCase));
}