Queryexpression for rapid CRM Development

Source: Internet
Author: User

/* Creator: the blog of caidao jushi
* Creation date: January 1, July 06, 2014
*/

Namespace net. CRM. organizationservice
{
Using system;
Using Microsoft. xrm. SDK;
Using Microsoft. xrm. SDK. query;
Using system. Collections. Generic;

/// <Summary>
/// Queryexpression
/// </Summary>
Public class queryexpressiondemo
{
/// <Summary>
/// Basic mode, construct query -- queryexpression
/// </Summary>
Public void createqueryexpression ()
{
Queryexpression query = new queryexpression ();
// Name of the queried object
Query. entityname = "new_accountproduct ";
// Query the object property set
Query. columnset = new columnset ("new_product", "new_name", "new_price ");
// Query Conditions
Query. Criteria. addcondition (New conditionexpression ("statecode", conditionoperator. Equal, 0 ));
Query. Criteria. addcondition (New conditionexpression ("new_name", conditionoperator. Like, "Fruit" + "% "));
List <guid> List = new list <guid> ();
List. Add (New GUID ("27bb1b31-09bb-e311-b590-d89d671782d3 "));
List. Add (New GUID ("E8F8D412-31BB-E311-B590-D89D671782D3 "));
Query. Criteria. addcondition (New conditionexpression ("new_product", conditionoperator. In, list. toarray ()));
Query. Criteria. addcondition (New conditionexpression ("new_name", conditionoperator. notnull ));
Query. Criteria. addcondition (New conditionexpression ("new_price", conditionoperator. null ));
}

/// <Summary>
/// In quick mode, construct the query -- queryexpression
/// </Summary>
Public void createquickqueryexpression ()
{
Queryexpression query = "new_accountproduct". toquery ("new_product", "new_name", "new_price ");
Query. addequal ("statecode", 0 );
Query. addlike ("new_name", "Fruit" + "% ");
List <guid> List = new list <guid> ();
List. Add (New GUID ("27bb1b31-09bb-e311-b590-d89d671782d3 "));
List. Add (New GUID ("E8F8D412-31BB-E311-B590-D89D671782D3 "));
Query. Addin ("new_product", list. toarray ());
Query. addnotnull ("new_name ");
Query. addnull ("new_price ");
}


}

Public static class extensionfunction
{
Public static queryexpression toquery (this string entityname)
{
Queryexpression query = new queryexpression ();
Query. entityname = entityname;
Return query;
}

Public static queryexpression toquery (this string entityname, Params string [] attrs)
{
Queryexpression query = new queryexpression ();
Query. entityname = entityname;
Query. columnset = new columnset (attrs );
Return query;
}

Public static void addequal <t> (this queryexpression query, string name, T value)
{
Query. Criteria. addcondition (New conditionexpression (name, conditionoperator. Equal, value ));
}

Public static void addlike (this queryexpression query, string name, string value)
{
Query. Criteria. addcondition (New conditionexpression (name, conditionoperator. Like, value ));
}

Public static void addin <t> (this queryexpression query, string name, t [] values)
{
Query. Criteria. addcondition (New conditionexpression (name, conditionoperator. In, values ));
}

Public static void addnotnull (this queryexpression query, Params string [] attrs)
{
If (attrs! = NULL & attrs. length> 0)
{
Foreach (string name in attrs)
{
Query. Criteria. addcondition (New conditionexpression (name, conditionoperator. notnull ));
}
}
}

Public static void addnull (this queryexpression query, Params string [] attrs)
{
If (attrs! = NULL & attrs. length> 0)
{
Foreach (string name in attrs)
{
Query. Criteria. addcondition (New conditionexpression (name, conditionoperator. null ));
}
}
}
}


}

Comparison:

 

 

 

 

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.