zoukankan      html  css  js  c++  java
  • Entityframework lambda Join Extension

        public class DictionaryResult<TKey, TValue>
        {
            public TKey Key { get; set; }
            public IEnumerable<TValue> Values { get; set; }
        }
    
        public class JoinResult<TLeft, TRight>
        {
            public TLeft Left { get; set; }
            public TRight Right { get; set; }
        }
        public static class JoinExtension
        {
            /// <summary>
            /// 两表JOIN,以Key匹配,仅返回交集
            /// </summary>
            /// <typeparam name="T1"></typeparam>
            /// <typeparam name="T2"></typeparam>
            /// <typeparam name="TKey"></typeparam>
            /// <typeparam name="TResult"></typeparam>
            /// <param name="query"></param>
            /// <param name="another"></param>
            /// <param name="keySelector"></param>
            /// <param name="keySelector2"></param>
            /// <param name="resultSelector"></param>
            /// <returns></returns>
            public static IQueryable<TResult> InnerJoin<T1, T2, TKey, TResult>(this IQueryable<T1> query,
                IQueryable<T2> another,
                Expression<Func<T1, TKey>> keySelector,
                Expression<Func<T2, TKey>> keySelector2,
                Expression<Func<T1, T2, TResult>> resultSelector
                )
            {
                return query.Join(another, keySelector, keySelector2, resultSelector);
            }
    
            /// <summary>
            /// 两表JOIN,左表为主,以Key匹配右表数据。结果表示方式以左表行为Key,右表行为Values
            /// </summary>
            /// <typeparam name="T1"></typeparam>
            /// <typeparam name="T2"></typeparam>
            /// <typeparam name="TKey"></typeparam>
            /// <param name="query"></param>
            /// <param name="another"></param>
            /// <param name="keySelector"></param>
            /// <param name="keySelector2"></param>
            /// <returns></returns>
            public static IQueryable<DictionaryResult<T1, T2>> LeftJoinToDictionary<T1, T2, TKey>(this IQueryable<T1> query,
                IQueryable<T2> another,
                Expression<Func<T1, TKey>> keySelector,
                Expression<Func<T2, TKey>> keySelector2
                )
            {
                return query.GroupJoin(another, keySelector, keySelector2, (x, y) => new DictionaryResult<T1, T2> { Key = x, Values = y });
            }
    
            /// <summary>
            /// 两表JOIN,左表为主,以Key匹配右表数据。结果必有左表所有行,右表未有匹配行时其对应属性为NULL
            /// </summary>
            /// <typeparam name="T1"></typeparam>
            /// <typeparam name="T2"></typeparam>
            /// <typeparam name="TKey"></typeparam>
            /// <typeparam name="TResult"></typeparam>
            /// <param name="query"></param>
            /// <param name="another"></param>
            /// <param name="keySelector"></param>
            /// <param name="keySelector2"></param>
            /// <param name="resultSelector"></param>
            /// <returns></returns>
            public static IQueryable<TResult> LeftJoin<T1, T2, TKey, TResult>(this IQueryable<T1> query,
                IQueryable<T2> another,
                Expression<Func<T1, TKey>> keySelector,
                Expression<Func<T2, TKey>> keySelector2,
                Expression<Func<JoinResult<T1, T2>, TResult>> resultSelector
                )
            {
                return query.LeftJoinToDictionary(another, keySelector, keySelector2)
                    .SelectMany(x => x.Values.DefaultIfEmpty(), (x, y) => new JoinResult<T1, T2> { Left = x.Key, Right = y })
                    .Select(resultSelector);
            }
    
            /// <summary>
            /// 两表JOIN,右表为主,以Key匹配左表数据。结果表示方式以右表行为Key,左表行为Values
            /// </summary>
            /// <typeparam name="T1"></typeparam>
            /// <typeparam name="T2"></typeparam>
            /// <typeparam name="TKey"></typeparam>
            /// <param name="query"></param>
            /// <param name="another"></param>
            /// <param name="keySelector"></param>
            /// <param name="keySelector2"></param>
            /// <returns></returns>
            public static IQueryable<DictionaryResult<T2, T1>> RightJoinToDictionary<T1, T2, TKey>(this IQueryable<T1> query,
                IQueryable<T2> another,
                Expression<Func<T1, TKey>> keySelector,
                Expression<Func<T2, TKey>> keySelector2
                )
            {
                return another.GroupJoin(query, keySelector2, keySelector, (x, y) => new DictionaryResult<T2, T1> { Key = x, Values = y });
            }
    
    
            /// <summary>
            /// 两表JOIN,右表为主,以Key匹配左表数据。结果必有右表所有行,左表未有匹配行时其对应属性为NULL
            /// </summary>
            /// <typeparam name="T1"></typeparam>
            /// <typeparam name="T2"></typeparam>
            /// <typeparam name="TKey"></typeparam>
            /// <typeparam name="TResult"></typeparam>
            /// <param name="query"></param>
            /// <param name="another"></param>
            /// <param name="keySelector"></param>
            /// <param name="keySelector2"></param>
            /// <param name="resultSelector"></param>
            /// <returns></returns>
            public static IQueryable<TResult> RightJoin<T1, T2, TKey, TResult>(this IQueryable<T1> query,
                IQueryable<T2> another,
                Expression<Func<T1, TKey>> keySelector,
                Expression<Func<T2, TKey>> keySelector2,
                Expression<Func<JoinResult<T1, T2>, TResult>> resultSelector
                )
            {
                return another.RightJoinToDictionary(query, keySelector2, keySelector)
                    .SelectMany(x => x.Values.DefaultIfEmpty(), (x, y) => new JoinResult<T1, T2> { Left = x.Key, Right = y })
                    .Select(resultSelector);
            }
    }
    
  • 相关阅读:
    ASP.NET 使用 X509Certificate2 系统找不到指定的文件
    SQL2000中TOP后不能使用变量
    补丁生成与应用工具 V1.5.4
    检测到通信错误。正在使用的通信协议:"TCP/IP"。正在使用的通信API:"SOCKETS"。检测到错误的位置:""。检测到错误的通信函数:"gethostbyname"。协议特定的错误代码:"*"、"11004"、"*"。 SQLST
    CLR 无法从 COM 上下文 0x1a2740 转换为 COM 上下文 0x1a28b0,这种状态已持续 60 秒。拥有目标上下文/单元的线程很有可能执行的是非泵式等待或者在不发送 Windows 消息的情况下处理一个运行时间非常长的操作
    Chart 控件 for vs2008的安装
    SqlServer孤立用户解决——"因为该用户存在对象,所以不能删除该用户。"
    函数 replace 的参数 1 的数据类型 text 无效。
    安装SqlServer2000出现"有挂起的操作"提示的解决
    DB2、ORACLE SQL写法的主要区别
  • 原文地址:https://www.cnblogs.com/mrtiny/p/10395936.html
Copyright © 2011-2022 走看看