LINQ to Oracle is too abnormal: functions such as contains should be written in reverse mode:

Source: Internet
Author: User

Context: saasreportprocontext CTX = factory. saasreportprocontextfactory. getsaasreportprocontext ();

There are three parameters: String reportname, string module, string type

In LINQ to SQL

VaR query = from o in CTX. reportor
WhereO. reportor_name.contains (reportname)
&&O. reportor_module.contains (module)
&&O. reportor_type.contains (type)
Select O;

Translate into SQL

Select * from XXX Where reportor_name like '% x % '.........

 

Write the following code on the fly in the LINQ to Oracle directory:

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 ))

In this case, I will replace Oracle with the SQL Server database, all queriesCodeIf you want to rewrite it, it is impossible to transplant it.

Originally, the eforacleprovider project is an open-source project that is better suited to the use of LINQ to Oracle. It seems that you have to modify and implement it on your own.

 

Later, I thought about the problem of updating and maintaining the open-source framework after it was updated. So I directly wrote the subordinate code to solve the problem:

 

1. Interface: ilikebridge

 

Code

  ///   <Summary>
/// This class is defined mainly to solve the problem of calling functions such as LINQ ins () and stratwith () in LINQ to Oracle, which is the opposite of calling functions such:
/// So building a bridge, all like passing through this bridge turn
/// When the database is changed to SQL or another database, modify this type or configuration.
///   </Summary>
Public   Interface Ilikebridge
{
Bool Likeleft ( String Field, String Value );

BoolLikeright (StringField,StringValue );

BoolLikeall (StringField,StringValue );
}

 

2. ora implementation

 

Code

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 implementation

 

Code

Public   Class Likebridgesql: ilikebridge
{
Public   Bool Likeleft ( String Field, String Value)
{
Return Field. startswith (value );
}

Public BoolLikeright (StringField,StringValue)
{
ReturnField. endswith (value );
}

Public BoolLikeall (StringField,StringValue)
{
ReturnField. Contains (value );
}
}

 

 

4. Factory

 

Code

Public   Class Likebridgefactory
{
Private   Static Ilikebridge Bridge =   Null ;
Public   Static Ilikebridge getlikebridge ()
{
If (Bridge =   Null )
{
Object OBJ = Assembly. getexecutingassembly (). createinstance (system. configuration. configurationmanager. deleettings [ " Likebridgeprovider " ]);
Bridge
= OBJ As Ilikebridge;
}
Return Bridge;
}
}

 

5. Add the key "likebridgeprovider" value = "xx. xx. likebridgeora" in X. config"

 

6. call:

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.