LINQ series (9)-end of Expression Tree (case study and summary)

Source: Internet
Author: User

Hi, I haven't updated it here for a long time. A lot of things happened during this time. I was sent to Denver, USA for two months and returned to my country around December 20 last year. In addition, in March this year, I switched to dongjia. Now I work in Shenzhen Moody's and continue to work in. NET development.

Now, go to the topic.

This section is the end of the expression tree. I will upload some cases I have written and make some summary. In fact, I used to train expression trees in two sections at center of expert (COE) at HP. I wrote the case in this section when training.

OK, first case

Case 1: dynamic query

In actual projects, we may have the following requirements:

For a local data source, we may want to dynamically determine the query conditions during runtime. For example, a textbox,

The local data source is a list. The data structure of each item in the list is defined as follows:

Public Class Flight
{
Public String Carriercode
{
Get ;
Set ;
}

Public string flightnumber
{< br> Get ;
set ;
}

Public string flightstartdate
{< br> Get ;
set ;
}

Public string departurestation
{< br> Get ;
set ;
}

Public StringAircrafttailnumber
{
Get;
Set;
}
}
# Endregion

Public EnumJoinfunction
{
Or,
And
}

Public EnumOperatorexpression
{
Equal,
Notequal
}
}

 

Now, we want to dynamically construct query conditions based on user input in the text box.

For example, carriercode = AA indicates that all flight with carriercode as AA is searched.

Carriercode = AA | flightnumber = 0001 indicates that flight with carriercode AA or flightnumber 0001 is found.

Flightnumber! = 0001 indicates that flight with the flightnumber not 0001 is found

Flightnumber! = 0001 & carriercode = Co indicates that flight with flightnumber not 0001 and carriercode as CO is found

This example solves the problem in this scenario. You can take a look at it and contact me if you have any questions. My contact information has been mentioned in the previous article.

/Files/flyedward/dynamicquery.rar

 

Case 2: verification.

In actual project development, we often need to verify whether a deep attribute at a level is empty. To verify this attribute, we often need to write a lot of tediousCodeStep by step whether the parent object of this attribute is null, So we often need to write code similar to the following:

Return This . Updatewbpanelinforq! = Null
&& This . Updatewbpanelinforq. pos! = Null
&& This . Updatewbpanelinforq. Pos. length> 0
&& This . Updatewbpanelinforq. Pos [ 0 ]! = Null
&&! String . Isnullorempty ( This . Updatewbpanelinforq. Pos [ 0 ]. Airlinevendorid );

Is there a way for us to write code similar to the following, and verify the object at each level when performing verification is empty?

Validateexpressiontree ( " Nullflight. Pos. airlinevendoridclass. airlinevendorid " , Nullflight)

For example, if we have written this code, we hope to verify whether the POS is null, whether airlinevendoridclass is null, and whether airlinevendorid is null.

This example solves this problem.

/Files/flyedward/validateproperty.rar

 

Okay. In this example, only the two are added.

The following is a summary of the Series

1. There are two sets of LINQ: ienumerable <t> for local data sources. Iqueryable <t> for Remote Data sources

2. The local data source can be directly substituted into the runtime logic. The remote data source must translate the runtime logic into the identifiable logic of the data source.

3. Only the remote data source requires the expression tree as the intermediate carrier for translation, but the logic of the local data source can also be stored using the expression tree structure.

4. the provider is responsible for translating the expression tree into the recognizable logic of the remote data source.

5. The expression tree contains two parts: Body and parameter.

6. This series provides a small tool for viewing the Expression Tree Structure during debugging.

7. Some cases are also listed in this series.

 

Well, I wrote this series here. I remember when I wrote the first article of this series, I was 21 years old and I was an intern of HP. Now I am in Shenzhen, the software engineer of Moody's. if you have any questions, contact me via email or QQ. Next I will write a series about Silverlight and prism. Thank you.

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.