Skip to content

Commit dd1be39

Browse files
committed
Eliminate .ToList() in IFilterable<>.Find()
1 parent 26d0ae8 commit dd1be39

File tree

4 files changed

+34
-68
lines changed

4 files changed

+34
-68
lines changed

Orm/Xtensive.Orm/Orm/Model/ColumnInfoCollection.cs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,16 @@ public sealed class ColumnInfoCollection : NodeCollection<ColumnInfo>,
2121
{
2222
#region IFilterable<ColumnAttributes,ColumnInfo> Members
2323

24-
public IEnumerable<ColumnInfo> Find(ColumnAttributes criteria)
25-
{
26-
return Find(criteria, MatchType.Partial);
27-
}
24+
public IEnumerable<ColumnInfo> Find(ColumnAttributes criteria) => Find(criteria, MatchType.Partial);
2825

29-
public IEnumerable<ColumnInfo> Find(ColumnAttributes criteria, MatchType matchType)
30-
{
31-
// We don't have any instance that has attributes == FieldAttributes.None
32-
if (criteria == ColumnAttributes.None)
33-
return Array.Empty<ColumnInfo>();
34-
35-
switch (matchType) {
36-
case MatchType.Partial:
37-
return this.Where(f => (f.Attributes & criteria) > 0).ToList();
38-
case MatchType.Full:
39-
return this.Where(f => (f.Attributes & criteria) == criteria).ToList();
40-
case MatchType.None:
41-
default:
42-
return this.Where(f => (f.Attributes & criteria) == 0).ToList();
43-
}
44-
}
26+
public IEnumerable<ColumnInfo> Find(ColumnAttributes criteria, MatchType matchType) =>
27+
criteria == ColumnAttributes.None
28+
? Array.Empty<ColumnInfo>() // We don't have any instance that has attributes == FieldAttributes.None
29+
: matchType switch {
30+
MatchType.Partial => this.Where(f => (f.Attributes & criteria) > 0),
31+
MatchType.Full => this.Where(f => (f.Attributes & criteria) == criteria),
32+
_ => this.Where(f => (f.Attributes & criteria) == 0)
33+
};
4534

4635
#endregion
4736

Orm/Xtensive.Orm/Orm/Model/FieldInfoCollection.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,14 @@ public sealed class FieldInfoCollection
2727
public IEnumerable<FieldInfo> Find(FieldAttributes criteria) => Find(criteria, MatchType.Partial);
2828

2929
/// <inheritdoc/>
30-
public IEnumerable<FieldInfo> Find(FieldAttributes criteria, MatchType matchType)
31-
{
32-
// We don't have any instance that has attributes == FieldAttributes.None
33-
if (criteria == FieldAttributes.None)
34-
return Array.Empty<FieldInfo>();
35-
36-
switch (matchType) {
37-
case MatchType.Partial:
38-
return this.Where(f => (f.Attributes & criteria) > 0).ToList();
39-
case MatchType.Full:
40-
return this.Where(f => (f.Attributes & criteria) == criteria).ToList();
41-
case MatchType.None:
42-
default:
43-
return this.Where(f => (f.Attributes & criteria) == 0).ToList();
44-
}
45-
}
30+
public IEnumerable<FieldInfo> Find(FieldAttributes criteria, MatchType matchType) =>
31+
criteria == FieldAttributes.None
32+
? Array.Empty<FieldInfo>() // We don't have any instance that has attributes == FieldAttributes.None
33+
: matchType switch {
34+
MatchType.Partial => this.Where(f => (f.Attributes & criteria) > 0),
35+
MatchType.Full => this.Where(f => (f.Attributes & criteria) == criteria),
36+
_ => this.Where(f => (f.Attributes & criteria) == 0)
37+
};
4638

4739
/// <inheritdoc/>
4840
public override void UpdateState()

Orm/Xtensive.Orm/Orm/Model/IndexInfoCollection.cs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,22 @@ public class IndexInfoCollection: NodeCollection<IndexInfo>,
2424
/// </summary>
2525
/// <param name="criteria">The criteria.</param>
2626
/// <returns>A sequence of found objects.</returns>
27-
public IEnumerable<IndexInfo> Find(IndexAttributes criteria)
28-
{
29-
return Find(criteria, MatchType.Full);
30-
}
27+
public IEnumerable<IndexInfo> Find(IndexAttributes criteria) => Find(criteria, MatchType.Full);
3128

3229
/// <summary>
3330
/// Finds <see cref="IndexInfo"/> objects by the specified criteria and match type.
3431
/// </summary>
3532
/// <param name="criteria">The criteria.</param>
3633
/// <param name="matchType">Type of the match.</param>
3734
/// <returns>A sequence of found objects.</returns>
38-
public IEnumerable<IndexInfo> Find(IndexAttributes criteria, MatchType matchType)
39-
{
40-
if (criteria == IndexAttributes.None)
41-
return Array.Empty<IndexInfo>();
42-
43-
switch (matchType) {
44-
case MatchType.Partial:
45-
return this.Where(f => (f.Attributes & criteria) > 0).ToList();
46-
case MatchType.Full:
47-
return this.Where(f => (f.Attributes & criteria) == criteria).ToList();
48-
case MatchType.None:
49-
default:
50-
return this.Where(f => (f.Attributes & criteria) == 0).ToList();
51-
}
52-
}
35+
public IEnumerable<IndexInfo> Find(IndexAttributes criteria, MatchType matchType) =>
36+
criteria == IndexAttributes.None
37+
? Array.Empty<IndexInfo>()
38+
: matchType switch {
39+
MatchType.Partial => this.Where(f => (f.Attributes & criteria) > 0),
40+
MatchType.Full => this.Where(f => (f.Attributes & criteria) == criteria),
41+
_ => this.Where(f => (f.Attributes & criteria) == 0)
42+
};
5343

5444

5545
// Constructors

Orm/Xtensive.Orm/Orm/Model/TypeInfoCollection.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -280,19 +280,14 @@ private TypeInfo FindAncestor(Type type)
280280
/// <returns><see cref="IEnumerable{TItem}"/> that contains all found instances.</returns>
281281
public IEnumerable<TypeInfo> Find(TypeAttributes criteria) => Find(criteria, MatchType.Partial);
282282

283-
public IEnumerable<TypeInfo> Find(TypeAttributes criteria, MatchType matchType)
284-
{
285-
if (criteria == TypeAttributes.None)
286-
return Array.Empty<TypeInfo>();
287-
switch (matchType) {
288-
case MatchType.Partial:
289-
return this.Where(f => (f.Attributes & criteria) > 0);
290-
case MatchType.Full:
291-
return this.Where(f => (f.Attributes & criteria) == criteria);
292-
default:
293-
return this.Where(f => (f.Attributes & criteria) == 0);
294-
}
295-
}
283+
public IEnumerable<TypeInfo> Find(TypeAttributes criteria, MatchType matchType) =>
284+
criteria == TypeAttributes.None
285+
? Array.Empty<TypeInfo>()
286+
: matchType switch {
287+
MatchType.Partial => this.Where(f => (f.Attributes & criteria) > 0),
288+
MatchType.Full => this.Where(f => (f.Attributes & criteria) == criteria),
289+
_ => this.Where(f => (f.Attributes & criteria) == 0)
290+
};
296291

297292
#endregion
298293

0 commit comments

Comments
 (0)