Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

技巧:ISelect 如何拷贝(copy)复用,克隆(clone) #644

Closed
2881099 opened this issue Jan 14, 2021 · 0 comments
Closed

技巧:ISelect 如何拷贝(copy)复用,克隆(clone) #644

2881099 opened this issue Jan 14, 2021 · 0 comments
Labels
docs This is a document

Comments

@2881099
Copy link
Collaborator

2881099 commented Jan 14, 2021

当一个 ISelect 构造到一定复杂程序之后,比如:

public void Test()
{
    var select1 = fsql.Select<AdmRoute>().Include(a => a.Parent)
        .WhereIf(!string.IsNullOrEmpty(key), a => a.Name.Contains(key) || a.Extdata.Contains(key) || a.Remark.Contains(key) || a.TenantId.Contains(key) || a.Parent.Name.Contains(key) || a.Parent.Extdata.Contains(key) || a.Parent.Remark.Contains(key) || a.Parent.TenantId.Contains(key))
        .WhereIf(Parent_Id?.Any() == true, a => Parent_Id.Contains(a.ParentId))
        .WhereIf(mn_Roles_Id?.Any() == true, a => a.Roles.AsSelect().Any(b => mn_Roles_Id.Contains(b.Id)));

    var select2 = select1;
    select1.Where(a => a.Status == 0);
    //此时 select2 也附加了 a.Status == 0 条件
}

利用语法糖解决:

public void Test()
{
    ISelect<AdmRoute> GetSelect() => fsql.Select<AdmRoute>().Include(a => a.Parent)
        .WhereIf(!string.IsNullOrEmpty(key), a => a.Name.Contains(key) || a.Extdata.Contains(key) || a.Remark.Contains(key) || a.TenantId.Contains(key) || a.Parent.Name.Contains(key) || a.Parent.Extdata.Contains(key) || a.Parent.Remark.Contains(key) || a.Parent.TenantId.Contains(key))
        .WhereIf(Parent_Id?.Any() == true, a => Parent_Id.Contains(a.ParentId))
        .WhereIf(mn_Roles_Id?.Any() == true, a => a.Roles.AsSelect().Any(b => mn_Roles_Id.Contains(b.Id)));

    var select1 = GetSelect();
    var select2 = GetSelect();
    select1.Where(a => a.Status == 0);
    //此时 select2 不会附加 a.Status == 0 条件
}

科普:

c# 7.0 支持本地函数,方法内再定义临时方法,这个特性向大家推荐,在很多时候都非常有效。

方法内还可以定义方法,那就称它:本地函数/嵌套方法。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This is a document
Projects
None yet
Development

No branches or pull requests

2 participants