terça-feira, 17 de setembro de 2013

Insert using LINQ

 Hi!

This is a method from a project I'm currently working at.

        public bool insert(string id, string companyName)
        {
            DataClasses1DataContext lObjDC = new DataClasses1DataContext();

            int lCount = lObjDC.Customers.Where(x => x.CustomerID == id).Count();




            //If customer doesn't exist.
            if (lCount == 0)
            {
                Customer lObjCTM = new Customer();
                lObjCTM.CustomerID = id;
                lObjCTM.CompanyName = companyName;
                lObjDC.Customers.InsertOnSubmit(lObjCTM);
                lObjDC.SubmitChanges();

                return true;
            }
            return false;
        }


PS: I should have written an exist method instead of using Count() inside the insert method.

Nenhum comentário:

Postar um comentário