Linq to oracle 太变态:Contains等函数要反着写:

上下文:SaaSReportProContext ctx = Factory.SaaSReportProContextFactory.GetSaaSReportProContext();

有三个参数:string reportName, string module, String type

在linq to sql中


            var query = from o in ctx.Reportor
                        where o.REPORTOR_NAME.Contains(reportName)
                        && o.REPORTOR_MODULE.Contains(module)
                        &&  o.REPORTOR_TYPE.Contains(type)
        select o;

翻译成sql

select * from xxx where REPORTOR_NAME like '%x%' .........

在linq to oracle中居然反着写:


            var query = from o in ctx.Reportor
                       where (String.IsNullOrEmpty(reportName) ? true : reportName.Contains(o.REPORTOR_NAME))
                        && (String.IsNullOrEmpty(module) ? true : module.Contains(o.REPORTOR_MODULE))
                        && (String.IsNullOrEmpty(type) ? true : type.Contains(o.REPORTOR_TYPE))

这样的话,到时候我将oracle又换成sql server数据库,所有的查询代码要重写了,根本没有移植可能了。

本来EFOracleProvider工程是一个比较好linq to oracle的开源工程,看来得自己下手修改实现了。

后来想了想,如果修改开源框架,导致以后开源框架更新后的更新维护问题,于是直接写了下属代码来处理这个问题:

 1,接口:ILikeBridge

代码
 /// <summary>
    
/// 定义此类主要是为了解决linq to oracle中的Contains(),StratWith()等函数的调用方法和linq to sql调用相反:具体:
    
/// 故建一座桥,所有like经过这座桥转过去
    
/// 当数据库换成sql或者别的数据库,修改此类或者配置即可
    
/// </summary>
    public interface ILikeBridge
    {
        
bool LikeLeft(string field, string value);

        
bool LikeRight(string field, string value);

        
bool LikeAll(string field, string value);
    }

2,ora的实现

代码
public class LikeBridgeOra : ILikeBridge
    {
        
public bool LikeLeft(string field, string value)
        { 
            
//oracle
            return (String.IsNullOrEmpty(value) ? true : value.StartsWith(field));
        }

        
public bool LikeRight(string field, string value)
        {
            
//oracle
            return (String.IsNullOrEmpty(value) ? true : value.EndsWith(field));
        }

        
public bool LikeAll(string field, string value)
        {
            
//oracle
            return (String.IsNullOrEmpty(value) ? true : value.Contains(field));
        }
    }

3,sql的实现

代码
public class LikeBridgeSql : ILikeBridge
    {
        
public bool LikeLeft(string field, string value)
        {
            
return field.StartsWith(value);
        }

        
public bool LikeRight(string field, string value)
        {
            
return field.EndsWith(value);
        }

        
public bool LikeAll(string field, string value)
        {
            
return field.Contains(value);
        }
    }
4,工厂
代码
public class LikeBridgeFactory
    {
        
private static ILikeBridge bridge = null;
        
public static ILikeBridge GetLikeBridge()
        {
            
if (bridge == null)
            {
                
object obj = Assembly.GetExecutingAssembly().CreateInstance(System.Configuration.ConfigurationManager.AppSettings["LikeBridgeProvider"]);
                bridge 
= obj as ILikeBridge;
            }
            
return bridge;
        }
    }

5,最后在X.config中加入key“LikeBridgeProvider” value = “xx.xx.LikeBridgeOra”

6,调用:

            var query = from o in ctx.Reportor                       
                        where Linq.LikeBridgeFactory.GetLikeBridge().LikeAll(o.REPORTOR_NAME,reportName)
                        && Linq.LikeBridgeFactory.GetLikeBridge().LikeAll(o.REPORTOR_MODULE, reportName)
                        && Linq.LikeBridgeFactory.GetLikeBridge().LikeAll(o.REPORTOR_TYPE, type)
                        select o; 

原文地址:https://www.cnblogs.com/sinxsoft/p/1777887.html