具有 Lambda/Selector/Predicate 逻辑的扩展方法

发布于 2024-11-15 02:05:03 字数 1043 浏览 2 评论 0 原文

我试图将以下逻辑放入扩展方法中,让我为字段的选择器传递一个 Func 。

public class MyClass {
    public decimal someValue {get; set;}
}

public class NumericSearch {
    decimal searchValue {get; set;}

    // Will be =, <=, >=, >, <
    string searchType {get; set;} 
}

... 

List<MyObject> listOfClass = { ... };

if (search.searchType == "=") {
    listOfClass = listOfClass.Where(l=>l.someValue == 123).ToList();
} else if (search.searchType == "<=") {
    listOfClass = listOfClass.where(l=>l.someValue <= 123).ToList();
} else if (...){
    ...
}

我的目标是能够像这样调用它:

var filteredList = listOfClass.applyNumericSearch(l=>l.someValue, new NumericSearch() { searchValue = 123, searchType = "<="} );

到目前为止,我的方法签名看起来像这样,但我不太确定如何处理 Lamda /selection 部分来实际完成我想要完成的工作:

public static IEnumerable<TSource> applyNumericSearch<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal> selector, NumericSearch search) {

I'm trying to put the following logic into an Extension method that let's me pass in a Func for the selector of the field.

public class MyClass {
    public decimal someValue {get; set;}
}

public class NumericSearch {
    decimal searchValue {get; set;}

    // Will be =, <=, >=, >, <
    string searchType {get; set;} 
}

... 

List<MyObject> listOfClass = { ... };

if (search.searchType == "=") {
    listOfClass = listOfClass.Where(l=>l.someValue == 123).ToList();
} else if (search.searchType == "<=") {
    listOfClass = listOfClass.where(l=>l.someValue <= 123).ToList();
} else if (...){
    ...
}

My goal is to be able to call it like this:

var filteredList = listOfClass.applyNumericSearch(l=>l.someValue, new NumericSearch() { searchValue = 123, searchType = "<="} );

So far, my method signature looks like this, but I'm not really sure how to handle the Lamda /selection portion to actually do the work I want done:

public static IEnumerable<TSource> applyNumericSearch<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal> selector, NumericSearch search) {

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扭转时空 2024-11-22 02:05:03
public static IEnumerable<TSource> applyNumericSearch<TSource>(
    this IEnumerable<TSource> source,
    Func<TSource, decimal> selector,
    NumericSearch search
) {
    var projection = source.Select(x => selector(x));
    if(search.searchType == "<=") {
        return projection.Where(y => y <= search.searchValue);
    }
    // etc.
}

此外,我强烈考虑将 searchType 设为枚举的实例,

public enum SearchType {
    LessThan,
    LessThanOrEqual,
    Equal,
    GreaterThan,
    GreaterThanOrEqual,
    NotEqual
 };

而不是使用 string

public static IEnumerable<TSource> applyNumericSearch<TSource>(
    this IEnumerable<TSource> source,
    Func<TSource, decimal> selector,
    NumericSearch search
) {
    var projection = source.Select(x => selector(x));
    if(search.searchType == "<=") {
        return projection.Where(y => y <= search.searchValue);
    }
    // etc.
}

Additionally, I strongly consider making searchType an instance of an enum

public enum SearchType {
    LessThan,
    LessThanOrEqual,
    Equal,
    GreaterThan,
    GreaterThanOrEqual,
    NotEqual
 };

instead of using string.

你怎么敢 2024-11-22 02:05:03

看一下 动态 LINQ 库。

如果您对 lambda 感到满意,只需使用不同的谓词即可:

Func<decimal, bool> BuildPredicate (NumericSearch search)
{
    switch (search.Kind) {
        case SearchKind.Equal:
            return (x => x == search.Value);
        case SearchKind.LessThan:
            return (x => x < search.Value);
        // ...
    }
}

// in ApplyNumericSearch
var predicate = BuildPredicate (search);
return source.Where (x => predicate (selector (x));

但这不适用于数据库。

Take a look at Dynamic LINQ library if you really want to use string queries.

If you're fine with lambdas, just use different predicates:

Func<decimal, bool> BuildPredicate (NumericSearch search)
{
    switch (search.Kind) {
        case SearchKind.Equal:
            return (x => x == search.Value);
        case SearchKind.LessThan:
            return (x => x < search.Value);
        // ...
    }
}

// in ApplyNumericSearch
var predicate = BuildPredicate (search);
return source.Where (x => predicate (selector (x));

This will not work for databases though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文