Create dynamic SPQuery in Sharepoint


string myDynamicQuery = "<Eq><FieldRef Name=\"Author\" /><Value Type=\"User\">" + web.CurrentUser.Name.ToString() + "</Value></Eq>";

object[] distinctPostIds = GetDistinctPostIds();


for (int i = 0; i < distinctPostIds.Length; i++)
{
     strDynamicQuery = "<Or>" + myDynamicQuery + "<Eq><FieldRef Name=\"ID\" /><Value Type=\"Counter\">" + distinctPostIds[i].ToString() + "</Value></Eq></Or>";
}

myDynamicQuery = "<Where>" + myDynamicQuery + "</Where><OrderBy><FieldRef Name=\"Created\" Ascending=\"False\" /></OrderBy>";


// Sample for multiple OR conditon:  
//<Where>
//<Or>
//<Or>
//<Or>
//<Or>
//        <Eq><FieldRef Name=\"ID\" /><Value Type=\"Counter\">1</Value></Eq>
//        <Eq><FieldRef Name=\"ID\" /><Value Type=\"Counter\">2</Value></Eq>
//</Or>
//        <Eq><FieldRef Name=\"ID\" /><Value Type=\"Counter\">3</Value></Eq>
//</Or>
//        <Eq><FieldRef Name=\"ID\" /><Value Type=\"Counter\">4</Value></Eq>
//</Or>
//        <Eq><FieldRef Name=\"ID\" /><Value Type=\"Counter\">6</Value></Eq>
//</Or>
//</Where>


SPQuery myQuery = new SPQuery();
myQuery.Query = myDynamicQuery;