Projects use LINQ (Summary)

Source: Internet
Author: User

Language integration query (LINQ) is a breakthrough innovation in Visual Studio 2008 and. NET Framework 3.5. It builds a bridge between the object field and the data field.

The last project used LINQ, so I was eager to learn about writing in the LINQ language. below is my personal summary of the technical points that I used in the project. What I said is not good. Please give me some advice.

Some people say that LINQ is the upgraded version of dataset in framework2.0,

 

Write it firstArticleBefore I, I would like to recommend you to write a LINQ tool, linqpad tool, http://www.linqpad.net/This address has been downloaded, the tool said it is also good, said good is not from the SQL statement into the LINQ syntax,

 

LINQ ing

Usually private system is used in the LINQ ing database file of LINQ. the emergence of data types such as nullable <int> may be difficult to understand. Here we map data based on your database fields, system. nullable <int> ing to your database field is a field of the int type that can be null. This type is in the C # syntax. When assigning a value to this field, there will be an int? Will report an error saying no reference error. This is a common problem in my project.

 

The common solution is: 1. Modify the database field, change the int type field to null, and give the default value. Remap the data table to the LINQ

2. redefine an object class and convert Int? Type is forcibly converted to int type.

 

The syntax operation of LINQ is delayed.

Using (dbtablesmappingdatacontext Dc = new dbtablesmappingdatacontext (_ connstring ))

{

// Any operation can be considered as a transaction that no longer needs to be opened.

}

LINQ setting field storage

Let's talk about the setting field in LINQ. Anyone who has used LINQ knows its advantages, such as designing a table, such as a student information table, such as learning hobbies, personality, and life experiences, I believe that our previous design philosophy is to design three fields into three nvarchar (8000) fields, or design some fields as text fields, but none of them are good.

Now we have LINQ. You can put all three fields in the setting field.(Note that the field name must be setting ),You can store data by setting the setting field to the image type.How can I use the C # syntax to store it ?????I will talk about it later. The disadvantage of using the setting field to store data is that it is complicated to use setting as the query condition when performing conditional queries.

 

Private   String _ Parameter description =   String . Empty; // Interest

Public   String Invalid description { Get { Return _ Parameter description ;} Set {_ INVALID description = Value ;}}

Partial VoidOnloaded ()
{
Byte[] Bytes=Setting. toarray ();
Csetting setting= NewCsetting (bytes );

Load (setting );
}

Partial   Void Onvalidate (system. Data. LINQ. changeaction action)
{
Csetting setting =   New Csetting ();
Save (setting );
Byte [] Bytes = Setting. tobytearray ();
Setting =   New Binary (bytes );
}

private void Save (csetting setting)
{< br> setting. set ( " DESC " , _ strong description);
}

Private VoidLoad (csetting setting)
{
_ Parameter description=Setting. Get ("Desc",String. Empty );
}

 

Union in LINQ (recursive query: Query all nodes under the parent node)

 

VaR Query = (From Item1 In DC. sysorganization
Where (Item1.fathersysorganizationid = Orgid) && Item1.isdel =   False
Select New Sysorganizationinfo {sysorganizationid = Item1.sysorganizationid, name = Item1.name, code = Item1.code, baseprovinceid = ( Int ) Item1.baseprovinceid, basecityid = ( Int ) Item1.basecityid })
. Union (
From item2 In DC. sysorganization
Join item3 In DC. sysorganization
On item2.fathersysorganizationid equals item3.sysorganizationid
Where (Item2.sysorganizationid = Orgid) && Item2.isdel =   False
Select New Sysorganizationinfo {sysorganizationid = Item2.sysorganizationid, name = Item2.name, code = Item2.code, baseprovinceid = ( Int ) Item2.baseprovinceid, basecityid = ( Int ) Item2.basecityid });

 

Join join subqueries in LINQ

VaR roles = From AA In (
From In DC. sysorganizationroleuser
Join B In DC. sysorganizationrole on A. sysroleid equals B. sysroleid
Join C In DC. sysorganizationrolefunctionitem on B. sysroleid equals C. sysroleid
Join d In DC. sysfunctionitem on C. sysfunctionitemid equals D. sysfunctionitemid
Join D2 In DC. sysfunctionitem on C. sysfunctionitemid equals d2.sysfunctionitemid
Where A. sysuserid = Userid
Select D)
From bb In DC. sysfunctionitem
Where AA. sysfunctionitemid = BB. sysfunctionitemid | AA. fatherfunctionitemid = BB. sysfunctionitemid
Select BB;

 

The defaultifempty () syntax is used for the left join query in LINQ,

 

 

VaR svcuser = From In HDC. sysservicecenteruser
Join B In HDC. sysservicecenter on A. sysservicecenterid equals B. sysservicecenterid
Join C In HDC. sysservicepositionuser on A. sysuserid equals C. sysuserid into pous
From C In Pous. defaultifempty ()
Join d In HDC. sysservicecenterposition on C. sysservicepositionid equals D. sysservicepositionid into Po
From d In Po. defaultifempty ()
Where (_ Sysservicecenterid =   0   ?   True : A. sysservicecenterid = _ Sysservicecenterid)
&& (_ Sysservicepositionid =   0   ?   True : (From W In HDC. sysservicepositionuser Where W. sysservicepositionid = _ Sysservicepositionid select W. sysuserid). Contains (A. sysuserid ))
&& (_ Name =   Null   | _ Name. Trim () =   ""   ?   True : A. fullname. Contains (_ name ))
&& (_ Code =   Null   | _ Code. Trim () =   ""   ?   True : A. Code = _ Code)
Select New SVC
{
_ Sysuserid = (A. sysuserid =   Null   ?   0 : A. sysuserid ),
_ Sysservicecenterid = (A. sysservicecenterid = Null ? 0 Int ) A. sysservicecenterid ),
// _ Sysservicepositionid = (INT) C. sysservicepositionid,
_ Svcname = B. Name,
// _ Positionname = D. Name,
_ Loginname = A. loginname,
_ Fullname = A. fullname,
_ Code = (A. Code = Null ? "" : A. Code)
};

 

Well, it's really hard to write a LINQ article.

 

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.