How do I use ORM to support SQL statements when a case?

Source: Internet
Author: User

How does oql support case?

Today, a friend asked me if OQL could support the case when statement? The example SQL he gave is as follows:

Select Hname, case if isenable=1then' enable '  else' deactivate ' from Tb_user

OQL is an ORM query Language for the sod framework, similar to LINQ, but it was born earlier than LINQ and is closer to SQL syntax. So, for the sod frame, how the ORM supports case, is tantamount to asking OQL how to support case.

This problem has more than this one friend to ask me, I think, or to solve this problem to write a blog, give everyone a new solution.
Noting that the term "new scheme" is not intended to allow OQL to support the nature of case, why should it do so?

    1. QL only solves 80% of ordinary queries, other complex queries, should rely on other technologies, otherwise it will increase the complexity of OQL;
    2. Complex query, can use the sql-map technique of sod frame, write SQL statement in Sqlmap.config file;
    3. You can use a database view or a computed column;


The first 3 reasons, the 2nd, 3 methods can also be seen as a solution to this problem, but they all need to add more work, if OQL can directly support or more convenient, so I am here today to give you a 4th solution:

Computed properties for entity classes

Don't say much nonsense, first look directly at the code:

 Public classuser:entitybase{ PublicUser () {TableName="Tb_user"; }   Public stringhname{Get{returngetproperty<string> ("Hname");} Set{setproperty<string> ("Hname", Value, -);} }   Public BOOLisenable{Get{returngetproperty<BOOL> ("isenable");} Set{setproperty<bool> ("isenable", value);} }     Public stringisenabledescrition {Get{        returnIsenable?"enabled":"Deactivate"     }   }}

Here, hname,isenable such a property, the sod frame is called " persisted attribute " because it maps the field isenable of the Tb_user table, which is called " persisted attribute field ".

The Get,set method of the persisted attribute uses the special method of the Sod entity class Setproperty,getproperty constructs, so the property isenabledescription it is not persisted property, but it takes advantage of isenable this persisted property, The persistence property is evaluated, so we call this property the " computed attribute " of the entity class.

Let's see if this "computational attribute" is good for SQL's case-when effect?

As long as you forget the database, do not encounter problems to think how to solve with SQL statements, is not the idea of enlightened?

How to use the calculated properties

As mentioned earlier, the "computed attribute" of an entity class is not inherently a "persisted property", it is a computation of persisted properties, which is very similar to the computed column above the SQL Server table.
Therefore, the use of "computed properties" on the sod frame has a principle that must be noted: "Computed properties" cannot appear in the OQL statement.

For example, you should use an entity class that contains computed columns as follows:

User user=new User () {hname=" Zhang San "}; var q=OQL. From (user)  . Select (user. Hname,user. isenable)  . Where (user. Hname). END; User user2= entityquery<user>. Queryobject (q); string isenabledesction =user2.isenabledesction;

About the above sod frame query use introduction, you can refer to the ". NET ORM" "Sod Honey"--0 Basic introductory article

Thank you for your support for the sod frame, for more information, please refer to pdf.net sod Open source Framework red Envelopes Delivery && Beginner Quick Start Guide

Open source projects need more support, sod Open source project website: http://pwmis.codeplex.com

How do I use ORM to support SQL statements when a case?

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.