C # construct a lambda expression using the expression tree in a generic class

Source: Internet
Author: User

The scenario has recently adjusted the crawler's database architecture and needs to migrate data to MongoDB. We need to implement a new Dao generic class for MongoDB. Okay, let's get started, the problem arises when the delete operation is implemented. Our Delete operation is defined as follows: void Delete (TEntity entity ). TEntity is our generic class. The delete operation that comes with the MongoDB official driver is as follows: // assume that the data model is defined as Articlevar query = Query <Article>. EQ (t => t. id, id); coll. remove (query); the Dao operation interface cannot be modified. Therefore, we must perform the following operations: get the Id value of the entity to construct a lambda expression to get the Id attribute. For example, you can use reflection to get the Id attribute. As for the 1st lambda expressions, you don't know how to create it. Search for information on the Internet and learn that the C # Lambda Expression Tree allows us to process Lambda expressions like processing data (such as reading and modifying data .. This has a direction. I have studied the knowledge of Expression Tree, and finally implemented it after great bumps. The lambda expressions I use are relatively simple and easy to construct. You can see the annotations in the Code. Code: // <summary> /// the Mongodb used, each data model must contain the Id attribute, delete an object using the Id attribute // </summary> /// <param name = "entity"> </param> public void Delete (TEntity entity) {var coll = _ db. getCollection <TEntity> (typeof (TEntity ). name); if (entity = null) {return;} ObjectId id = (ObjectId) typeof (TEntity ). getProperty ("Id "). getValue (entity, null); // construct a lambda expression {t => t. id} // construct the call target t var target = Expression. parameter (typeof (TEntity), "t"); // construct the Expression MemberExpression bodyExp = Expression for the attribute Id of t. property (target, "Id"); // construct a complete lambda Expression <Func <TEntity, ObjectId> selector = Expression. lambda <Func <TEntity, ObjectId> (bodyExp, new [] {target}); // use the statement before generics: Query <Article>. EQ (t => t. id, id); var query = Query <TEntity>. EQ (selector, id); coll. remove (query );}

Related 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.