/* 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: