Why does C # Lambda expressions not support statements? (You have no answer .)

Source: Internet
Author: User

Well, don't drop eggs first. The Lambda expression I am talking about refers to expression <t>, not any item => {...}. Well, if you still don't understand it, I will give you an example:

(Sorry, the original return item; is a written mistake and has been corrected to return (item % 2) = 0;

Code

Iqueryable < Int > Values =   New   Int [] { 1 }. Asqueryable ();
Int Last =   0 ;
VaR Query = Values. Where (
Item => {
Last = Item;
Console. writeline ( " Last value = {0} " , Last );
Return (Item % 2) = 0;
}
);
VaR result = Query. toarray ();

 

Compilation fails as long as you run in C #4.0 or earlier versions. If you want to know what the compiler prompts, please do the experiment yourself.

(Note: I know there is no blockexpression in 3.5, and so on. These things are added in FX 4.0. I mean, why does C #4.0 not support syntaxes. Some people say it may not catch up. This is a bit reliable, but so far, I still haven't seen the URL. If you want to know it, you can share it with others .)

 

Now, the question is: Why not? I have not found any answers to this question for the moment. Maybe you can provide standard answers. Of course, I also have some guesses:

1. The syntax may conflict with item => item, but it also solves the problem well: As long as statement is restricted, curly brackets must be used;

2. Some queryproviders may not be able to execute statements, such as objectquery in entityframework. However, I didn't think there was any major problem. Just throw an exception. Of course, this may indeed cause someCodeCompilation is successful, but an exception is thrown during execution. You do not know where the query is constructed. In this case, you only need to check the statement as soon as possible to prevent the problematic statement from going too far away.

 

I don't know if F # supports statements contained in lambda expressions. If I support the statements, I have to think about whether to convert them into a language. This causes a lot of trouble for some extensions. For example, I originally thought like this:

(This part of the code below is wrong again. It turns out to be categoryid = xxx, with fewer items .)

Code

VaR Query = From item In Pages
Where Item. categoryid =   1
Select item;
Iqueryable < Int > Updateaffactedline = Query. Update (
Item => {
Item. categoryid = Getcategoryid (newcategorydropdownlist );
Item. lastmodifydate = datetime. now;
}

);

Well, you might say, how can this be executed? Well, if you don't know the expression or the expressionvisitor, you can close this post. If you understand this, you will probably understand it:

1. valuation is required before execution. For example, you can write a statement similar. the norminator and partialevaluator in the net class library (see the method in enumerablequery), so that the actual value of the expressions like getcategoryid (newcategorydropdownlist) can be calculated and changed to a constantexpression;

2. After valuation, write a provider (or use another method) and convert the expression tree to the following SQL statement:

Update photo set categoryid = @ P0, lastmodifydate = @ P1 where categoryid = 1

 

Because C # does not support such a syntax, it can only be written in a very ugly format:

(In addition, some people say it is no ugly. It is not a few characters longer than the previous solution. The problem is that being ugly is not only a matter of how many times your nose is long, but also ugly. Here, the ugly thing is that the surface meaning of the code is "to generate a new object and initialize some attributes", but it is not actually the meaning, but to modify some attributes of some existing objects. "One" and "some", "new object" and "modification" are not consistent. I think this is ugly .)

Code

VaR Query = From item In Pages
Where Item. categoryid =   1
Select item;
Iqueryable < Int > Updateaffactedline = Query. updateuugly (
() =>   New Page (){
Categoryid = Getcategoryid (newcategorydropdownlist ),
Lastmodifydate = Datetime. Now,
}
);

 

In the above code, you can forget it. Sometimes some problems cannot be solved, such ...... Forget it. I think it's too deep. You will be dizzy. We still pull back the focus of this post:

Damn C # Why is expression <t> not supported with statement syntax? Let's have a heated discussion. (Why can't I solve the problem? I will mention it in my reply later, but I am not going to make it clear here. This is not just a sentence or a sentence .)

 

(Well, every time I think of it, it's annoying. It hinders some of my designs and has to make a lot of ugly. Then I will think of someone who regards the beauty of the language as XX, and even like to mix these problems with execution performance ...... Oh, it's far again .)

 

(Supplement: this is the case. Some people are talking about execution performance. The problem is that I am not going to discuss this issue here .)

 

 

 

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.