serving the solutions day and night

Pages

Monday, April 22, 2013

CRM Linq

//PickList values
private Dictionary<String, String> PickList(String entityName, String attID, string attName)
{
    Dictionary<String, String> dic = new Dictionary<String, String>();
    IOrganizationService service = (IOrganizationService)serviceProxy;
    QueryExpression qryExp = new QueryExpression(entityName);
    qryExp.ColumnSet = new ColumnSet(attName);
    EntityCollection entCollection = service.RetrieveMultiple(qryExp);
    dic = entCollection.Entities
        .Where(e => (e.Attributes.ContainsKey(attName)))
        .ToDictionary(e => e.Attributes[attID].ToString(), e => e.Attributes[attName].ToString());
    /*int i=1;
    foreach (var c in entCollection.Entities)
    {
        if (c.Attributes.ContainsKey(attName))
        {
        dic.Add(c.Attributes[attID].ToString(), c.Attributes[attName].ToString());
        }
    }*/
}

//Option Set values
dicSuffix = CRM_Food_OptionSet("Contact - Suffix");
private Dictionary<String, String> CRM_Food_OptionSet(String listName)
{
    CRMDataContext db = new CRMDataContext();
    return (from pl in db.dns_foodcrmpicklists
        join plv in db.dns_foodcrmpicklistvalues on pl.dns_foodcrmpicklistId equals plv.dns_FoodCRMPicklistId
        where pl.dns_name == listName && plv.dns_FoodValue != null
        select plv).ToDictionary(plv => plv.dns_CRMPicklistValue.ToString(), plv => plv.dns_FoodValue);
    #region "Old"
    /*var qry = from pl in db.dns_foodcrmpicklists
          join plv in db.dns_foodcrmpicklistvalues on pl.dns_foodcrmpicklistId equals plv.dns_FoodCRMPicklistId
          where pl.dns_name == listName
          select new
          {
          plv.dns_FoodString,
          plv.dns_FoodValue,
          plv.dns_crmpicklistname,
          plv.dns_CRMPicklistValue
          };

    foreach (var vt in qry)
    {
    if (vt.dns_FoodValue != null)
    {
        dic.Add(vt.dns_CRMPicklistValue.ToString(), vt.dns_FoodValue.ToString());
    }
    }*/
    #endregion "Old"
}

//Check PickList Value or not
private int CheckOSNull(String dbValue, Dictionary<String, String> dic)
{
    int rval = -1;
    if (dbValue != null && dbValue.Length > 0)
    {
    rval = Int32.Parse(dic.Single(x => x.Value == dbValue).Key);
    }
    return rval;
}


Find Last Updated ID
private int CRM_LastUpdatedFoodID()
{
    int lastFoodID = 0;
    try
    {
    CRMDataContext db = new CRMDataContext();
    string lvid = (from c in db.CRMContacts
           where c.Description == "Fast Food"
           orderby c.dns_FoodID descending
           select c.dns_FoodID).FirstOrDefault().ToString();
    if (lvid != null && lvid != string.Empty)
        lastFoodID = Int32.Parse(lvid);
    }
    catch (Exception e)
    {
    }

    return lastFoodID;
}


Select Query, Join, Order, Where and Top
private void Food_CRM()
{
    FoodDataContext db = new FoodDataContext();
    var qry = from fcr in db.FoodRegs
            join v in db.Food on fcr.FoodID equals v.FoodID
            join ft in db.FoodType on new {v.PlaceID, stc = "FastFood" } equals new { ft.PlaceID, stc = ft.FoodTypeCode }
            join ti in db.TypeItems on new { ft.TypeItemID, vid = v.FoodID} equals new { ti.TypeItemID, vid = ti.FoodID}
          where
            fcr.Status == "Fast Food"
            && ft.PlaceID > 10
          orderby fcr.FoodID
          select new
          {
              v.FoodD,
              fcr.FoodCode,
              j.PlaceName,
              ti.FoodTypeID
          };//).Take(1000);

    //Debug.WriteLine(qry.ToList().FirstOrDefault().ApplicationDate);
    //var vo = qry.ToList();

    foreach (var vt in qry)
    {
        vt.FoodD;
        vt.FoodCode;
        vt.PlaceName;
        vt.FoodTypeID;
    }   
}   


//Does Food already exist?
String fiid = xrmService.dns_foodsSet
   .Where(fis => (fis.dns_fooditemid == ftid) && (fis.dns_footid == vm.dns_footid))
   .Select(fis => fis.dns_fooditemid).SingleOrDefault().ToString();

No comments: