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 动态 Include 代码实现 #278

Closed
2881099 opened this issue Apr 16, 2020 · 0 comments
Closed

ISelect 动态 Include 代码实现 #278

2881099 opened this issue Apr 16, 2020 · 0 comments
Labels
good first issue Good for newcomers

Comments

@2881099
Copy link
Collaborator

2881099 commented Apr 16, 2020

当 T 未知的时候,可以使用该方法传入属性字符串

fsql.Select<Tag>()
    .Include(a => a.Parent.Parent)
    .ToList();

fsql.Select<Tag>()
    .Include("Parent.Parent")
    .ToList();

SQL:

SELECT ...
FROM [Tag] a 
LEFT JOIN [Tag] a__Parent ON a__Parent.[Id] = a.[Parent_id] 
LEFT JOIN [Tag] a__Parent__Parent ON a__Parent__Parent.[Id] = a__Parent.[Parent_id] 
/// <summary>
/// 动态 Include,如:fsql.Select&lt;Tag&gt;().Include("Parent.Parent").ToList();
/// </summary>
/// <typeparam name="T1"></typeparam>
/// <param name="select"></param>
/// <param name="navigateSelector"></param>
/// <returns></returns>
public static ISelect<T1> Include<T1>(this ISelect<T1> select, string navigateSelector) where T1 : class
{
    var s0p = (select as Select0Provider);
    var parmExp = Expression.Parameter(typeof(T1), s0p._tables[0].Alias);
    var currentType = typeof(T1);
    Expression currentExp = parmExp;
    foreach (var tmp1 in navigateSelector.Split('.'))
    {
        if (s0p._commonUtils.GetTableByEntity(currentType).Properties.TryGetValue(tmp1.Trim(), out var prop) == false)
            throw new ArgumentException($"{currentType.DisplayCsharp()} 无法找到属性名 {tmp1}");
        currentType = prop.PropertyType;
        currentExp = Expression.MakeMemberAccess(currentExp, prop);
    }
    if (currentExp == parmExp) return select;
    var navigateSelectorLambda = Expression.Lambda(
        typeof(Func<,>).MakeGenericType(typeof(T1), currentType),
        currentExp,
        parmExp);
    select.GetType().GetMethod("Include").MakeGenericMethod(currentType)
        .Invoke(select, new object[] { navigateSelectorLambda });
    return select;
}
@2881099 2881099 added good first issue Good for newcomers and removed good first issue Good for newcomers labels Apr 16, 2020
@2881099 2881099 closed this as completed May 1, 2020
@2881099 2881099 added the good first issue Good for newcomers label May 20, 2020
2881099 pushed a commit that referenced this issue Sep 18, 2020
2881099 pushed a commit that referenced this issue Sep 23, 2020
2881099 added a commit that referenced this issue Nov 4, 2020
2881099 added a commit that referenced this issue Nov 24, 2020
2881099 added a commit that referenced this issue Jun 9, 2022
2881099 added a commit that referenced this issue Jun 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant