Linqtonhibernate also makes me depressed: expression parameters cannot be used.

Source: Internet
Author: User

As mentioned above, there is a trap in linqtonhibernate's datetime processing. If you think about it, it should be not just for datetime, but for linqtonhibernate, you can only deal with the property level mapped to HBM, and there will be some inexplicable Problems When retrieving the property of the property.

This is another problem we encountered when writing code today: expression parameters cannot be used.

Let's take a look at my original code:

Code Public iqueryable <childentity> findforivoc (string Bu, guid centreid, string name, string IDNO, ienumerable <guid> programids
, Guid? Serviceid, string classname)
{
Return session. LINQ <childentity> (entityname)
. Where (child =>
Child. centre. Id. Equals (centreid)
& Child. Name. Contains (name)
& Child. IDNO. Contains (IDNO ))
. Where (joinprogram (programids, now ))
. Where (joinservice (serviceid, now ))
. Where (joinclass (classname, now ))
. Orderby (orderbyidno ())
. Thenby (orderbyid ());
}

 

To facilitate the use of some of the where clauses in the future, I naturally set up several methods such as joinprogram, joinservice, and joinclass to return the expression <func <childentity, bool> object.

Then, I call count () on the iqueryable <tentity> object, and an exception occurs: expression argument must be of Type icollection.

After searching for this exception message, there are not many results, but I mentioned it here. The solution is to change iqueryable to icollection... ...

Review the exception Stack

Code [Invalidoperationexception: expression argument must be of Type icollection.]
Nhibors. LINQ. Visitors. whereargumentsvisitor. getcollectioncontainscriteria (expression list, expression containedexpr) + 194
Nhib.pdf. LINQ. Visitors. whereargumentsvisitor. visitmethodcall (methodcallexpression expr) + 388
Nhib.pdf. LINQ. Visitors. expressionvisitor. Visit (expression exp) + 575
Nhib.pdf. LINQ. Visitors. nhibernateexpressionvisitor. Visit (expression exp) + 274
Nhib.pdf. LINQ. Visitors. whereargumentsvisitor. visitandalsoexpression (binaryexpression expr) + 96
Nhib.pdf. LINQ. Visitors. whereargumentsvisitor. visitbinary (binaryexpression expr) + 52
Nhib.pdf. LINQ. Visitors. expressionvisitor. Visit (expression exp) + 194
Nhib.pdf. LINQ. Visitors. nhibernateexpressionvisitor. Visit (expression exp) + 274
Nhib.pdf. LINQ. Visitors. whereargumentsvisitor. getexistscriteria (methodcallexpression expr) + 493
Nhib.pdf. LINQ. Visitors. whereargumentsvisitor. visitmethodcall (methodcallexpression expr) + 1234
Nhib.pdf. LINQ. Visitors. expressionvisitor. Visit (expression exp) + 575
Nhib.pdf. LINQ. Visitors. nhibernateexpressionvisitor. Visit (expression exp) + 274
Nhib.pdf. LINQ. Visitors. expressionvisitor. visitconditional (conditionalexpression c) + 62
Nhib.pdf. LINQ. Visitors. expressionvisitor. Visit (expression exp) + 319
Nhib.pdf. LINQ. Visitors. nhibernateexpressionvisitor. Visit (expression exp) + 274
Nhib.pdf. LINQ. Visitors. expressionvisitor. visitlambda (lambdaexpression lambda) + 21
Nhib.pdf. LINQ. Visitors. expressionvisitor. Visit (expression exp) + 639
Nhib.pdf. LINQ. Visitors. nhibernateexpressionvisitor. Visit (expression exp) + 274
Nhib.pdf. LINQ. Visitors. whereargumentsvisitor. visitunary (unaryexpression expr) + 34
Nhib.pdf. LINQ. Visitors. expressionvisitor. Visit (expression exp) + 140
Nhib.pdf. LINQ. Visitors. nhibernateexpressionvisitor. Visit (expression exp) + 274
Nhib.pdf. LINQ. Visitors. rootvisitor. handlewherecall (methodcallexpression call) + 92
Nhib.pdf. LINQ. Visitors. rootvisitor. visitmethodcall (methodcallexpression expr) + 907
Nhib.pdf. LINQ. Visitors. expressionvisitor. Visit (expression exp) + 575
Nhib.pdf. LINQ. Visitors. nhibernateexpressionvisitor. Visit (expression exp) + 274
Nhib.pdf. LINQ. Visitors. rootvisitor. visitmethodcall (methodcallexpression expr) + 53
Nhib.pdf. LINQ. Visitors. expressionvisitor. Visit (expression exp) + 575
Nhib.pdf. LINQ. Visitors. nhibernateexpressionvisitor. Visit (expression exp) + 274
Nhib.pdf. LINQ. Visitors. nhibernatequerytranslator. translateinternal (expression) + 81
Nhib.pdf. LINQ. Visitors. nhibernatequerytranslator. Translate (expression, queryoptions) + 43
Nhib.pdf. LINQ. nhibernatequeryprovider. translateexpression (expression) + 575
Nhib.pdf. LINQ. nhibernatequeryprovider. Execute (expression) + 17
Nhib.pdf. LINQ. queryprovider. system. LINQ. iqueryprovider. Execute (expression) + 13
System. LINQ. queryable. Count (iqueryable '1 source) + 310
Snbusinesslogic. serviceimpl. pager. page (iqueryable '1 entities, int32 startrow, int32 maxresult) in D: \ Singapore \ skoolnet \ SRC \ trunk \ skoolnet \ snbusinesslogic \ serviceimpl \ pager. CS: 12
Snbusinesslogic. serviceimpl. childserviceimpl. findforivoc (guid centreid, string name, string IDNO, ienumerable '1 programids, nullable '1 serviceid, string classname, int32 startrow, int32 maxresult) in D: \ Singapore \ skoolnet \ SRC \ trunk \ skoolnet \ snbusinesslogic \ serviceimpl \ childserviceimpl. CS: 49
Snusercontrols. Child. searchchild. loadsearchresults () in D: \ Singapore \ skoolnet \ SRC \ trunk \ skoolnet \ snusercontrols \ child \ searchchild. ascx. CS: 101
Snusercontrols. Child. searchchild. btnsearch_click (Object sender, eventargs E) in D: \ Singapore \ skoolnet \ SRC \ trunk \ skoolnet \ snusercontrols \ child \ searchchild. ascx. CS: 71
System. Web. UI. webcontrols. imagebutton. onclick (imageclickeventargs e) + 98
System. Web. UI. webcontrols. imagebutton. raisepostbackevent (string eventargument) + 161
System. Web. UI. Page. raisepostbackevent (ipostbackeventhandler sourcecontrol, string eventargument) + 29
System. Web. UI. Page. processrequestmain (Boolean includestagesbeforeasyncpoint, Boolean includestagesafterasyncpoint) + 2981

 

Exceptions are thrown when the WHERE clause is processed. To locate the error, only the WHERE clause whose parameter is func is left. OK. The above error occurs when a where clause is added.

I found it online. Although no clear description was found, multiple where statements in the example are indeed using the expression tree to merge multiple expressions into one expression and pass them into the WHERE clause.

As far as my understanding is concerned, the extensive use of expression trees can cause great damage to the code readability. I still honestly let all of my methods return func <childentity, bool> then call the delegate when spelling the where statement. For example, when I change the code to this, the code can be executed smoothly.

Code Public iqueryable <childentity> findforivoc (string Bu, guid centreid, string name, string IDNO, ienumerable <guid> programids
, Guid? Serviceid, string classname)
{
VaR entityname = Bu + childentity. staticentityclassname;
VaR now = datetime. now;
Return session. LINQ <childentity> (entityname)
. Where (child =>
Child. centre. Id. Equals (centreid)
& Child. Name. Contains (name)
& Child. IDNO. Contains (IDNO)
& Joinprogram (programids, now). Invoke (child ));
//. Where (joinservice (serviceid, now ))
//. Where (joinclass (classname, now ))
//. Orderby (orderbyidno ())
//. Thenby (orderbyid ());
}

 

In short, linqtonhibernate still cannot be used as conveniently as linqtosql. I don't know what will happen to the built-in LINQ provider of nh3.0? The alpha1 of nh3.0 has been released last weekend. We look forward to the official version ~~~

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.