Do you separate your LINQ queries into extension methods

In my current project, we set some goals for the performance indicators "Health Index" and "Cyclomatic complexity". The health index should be 60 or higher and Cyclometic Complexity 25 or less. We know that a performance index of 60 and above is pretty high.

We also use a lot of linq to filter / group / select objects. I found out that these linq queries do not score so high in the health index. The theses of these queries in extension methods give me a higher health index, which is good. But in most cases, extension methods are no longer universal, because I use them with my types instead of generic types.

For example, the following linq-query vs extension method:

Linq request

List.Where(m => m.BeginTime >= selectionFrom && m.EndTime <= selectionTo)

Extension Method:

public static IEnumerable<MyType> FilterBy(this IEnumerable<MyType> source, DateTime selectionFrom, DateTime selectionTo)
{
    return (IEnumerable<MyType>)source.Where(m => m.BeginTime >= selectionFrom && m.EndTime <= selectionTo);
}

List.FilterBy(selectionFrom, selectionTo);

The extension method gives me an improvement in the health index of 6 points and gives me a nice smooth syntax. On the other hand, I have to add a static class, but not a generic one.

Any ideas on which approach will benefit you? Or maybe there are different ideas on how to refactor linq queries to improve the health index?

+5
source share
5 answers

. , , , , .

, . , , .. . , , .

, . , , , , vs API ..

, , , ...

, LINQ , EnumerableExtensions in Extensions . , , .

, , . ! , , , . , IEnumerable<MyType>, this IEnumerable<MyType>. , . .

, . , , .

+5

... ! . .

, , ?

+4

. .

, . . psuedo:

var x = _repository.Customers().WhichAreGoldCustomers();

var y = _repository.Customers().WhichAreBehindInPayments();

, , " , ". , , " ".

, :

var z = _repository.Customers().WhichAreGoldCustomers().WhichAreBehindInPayments();

.

, , , ReSharper, Intellisense . ".Whic", , , "" , - , . ReSharper , ... nah:)

+2

NO: - .

, . :

List.Where(m => m.BeginTime >= selectionFrom && m.EndTime <= selectionTo)

:

List.FilterBy(selectionFrom, selectionTo);

, , - . , "FilterBy", - .

, , .

+1

, Payment PaymentLinqExtensions, .

. , , .

, , , , IHaveADate ( - : -)

public static IQueryable<T> WithinDateRange(this IQueryable<T> source, DateTime from, DateTime to) where T:IHaveADate

(IQueryable . , IEnumerable , . , SQL, , . LINQ, , , )

, , "EndDate" , DateRange.

public static IQueryable<T> WithinDateRange(this IQueryable<T> source, DateRange range) where T:IHaveADate

, ,

public static IEnumerable<T> WithinDateRange(this IEnumerable<T> source, DateRange range, Func<DateTime,T> getDate)

but this is more like DateRange to me. I do not know how much this will be used, although your situation may change. I found that getting too generic can make it harder to understand, and LINQ can be hard to debug.

var filtered = myThingCollection.WithinDateRange(myDateRange, x => x.Date)
0
source

All Articles