A simple example of using db4o in combination with LINQ and lambda expressions

Source: Internet
Author: User

Most queries that come into contact with db4o start with "query by example". This query method is simple but inconvenient, and its functions and performance are also very limited. The following is an example of an official sample query:

// Retrievepilotbynamepilot proto = new pilot ("Michael Schumacher", 0); objectset result = dB. Get (PROTO); listresult (result );

This query requires that we create a sample first, and then hand it to db4o to return Qualified Data Based on the sample. The main inconvenience lies in the processing of default class values and the query of combined conditions, at the same time, its execution performance is very poor.

And when db4o starts to support. after net 3.5, we had a simpler, more convenient, and more powerful choice. Let's take a look at db4o and. net 3.5 Sparks:

 

Preparations

First, create a command line application.Program.

Write a simple "student" class:

Public class student
{
Static random r = new random ();

Public student (INT student ID)
{
This. Student ID = student ID;
Gender = R. Next (2) = 1;
Score = R. Next (101 );
}

Public int student ID {Get; set ;}
Public bool gender {Get; set ;}
Public int score {Get; set ;}

Public override string tostring ()
{
Return string. Format ("student ID: {0: 0000} Gender: {1} score: {2}", student ID, gender? "Male": "female", score );
}
}

 
This class is used to simulate student data. both gender and score are randomly generated during construction.
 
Then introduce the DLL corresponding to. Net 3.5 for db4o:
 
 
Introduce namespace:

Using db4objects. db4o;
Using db4objects. db4o. LINQ;

 
Write in Main FunctionCode:

VaR DB = db4ofactory. openfile ("DB. db4o ");

For (INT I = 0; I <5000; I ++)
{
DB. Store (new student (I ));
}

 
This code is used to open the database and store 5000 student data to the database.
 
 
LINQ Query

Then write the LINQ query code to save the queried set to variable L:

VaR L = (from student Q in dB
Where! Q. Gender & Q. Score = 100
Orderby Q. Student ID descending
Select Q). Take (5 );

Here we query all girls with 100 scores, and sort the results in descending order by student ID, taking the top five.

From this we can see that db4o is very closely integrated with LINQ, and we have no sense of incompatibility when using LINQ queries, which is still handy.

Then traverse and output the result:

Foreach (var f in L)
{
Console. writeline (f );
}

Console. Readline ();

Query result output:

 

Lambda expression Query

In fact, this has been supported before the support of LINQ, or the support of lambda expressions is not the work scope of db4o developers.

We know that Lambda expressions are actually a delegate, while native queries of db4o requires a delegate. when a Lambda expression is introduced to the. NET Framework, it can be used in db4o queries. The following is an example of an official native query:

Ilist <pilot> pilots = dB. query <pilot> (delegate (pilot) {return pilot. Points = 100 ;});

You can easily change it to the lambda expression format:

Ilist <pilot> pilots = dB. query <pilot> (q => q. Points = 100 );

This is much more concise, right?

Likewise, the above LINQ query can also be changed:

VaR L = dB. query <student> (q =>! Q. Gender & Q. Score = 100). orderbydescending (q => q. Student ID). Take (5 );

The result is equivalent, which is easier to write and recommended for use.

 

Conclusion

Db4o is very good and powerful. Now many top enterprises are using it. It has brought about a qualitative innovation in development efficiency and data persistence. It is definitely worth learning and using.

Recommended learning materials: db4o7.0 Guide (translated by hankjin, Jeff, lixianfeng, Richard. liangyx, and cleverpig)

 

Download

ExampleSource code: Http://www.uushare.com/user/icesee/file/3490887

XPS version: http://www.uushare.com/user/icesee/file/3490892

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.