locked
UPDATE in Linq RRS feed

  • Question

  • User-83227557 posted

    Hi

    I want to update a field in my table to false. then do an if statement so if user doesnt exist in DB add userinterest with the active field set to true or else get userinterest and update active to true. 

    so far i have come up with this which i dont think is quite right needs some fixing. ;

       //Fix FROM HERE
                    if (!user.UserInterests.Any(ui => ui.InterestsID == intrst.InterestsID))
                    {
                            Uintrst = new UserInterest();
    
                            Uintrst.InterestsID = UserInterest;
                            Uintrst.UserInterestID = UserInterest;
                            Uintrst.Active = UserInterest.Active;
    
                            context.UserInterests.AddObject(Uintrst);
                    }
                    else
                    {
                            Uintrst = new UserInterest.Single();
                            Uintrst = new UserInterest();
    
                            UInterest.Active = true;
                    }
                    //TO HERE...

    
    
    
    
    below is the full coding if anyone needs to see it. 
    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using GeoCoding.Google;
    using GeoCoding;
    using System.Web.Configuration;
    
    namespace MVCFacebookTestApp.Models
    {
    
        public class Home
        {
            //public string UserID { get; set; }
            public string FBID { get; set; }
            public string AccessToken { get; set; }
            public string FName { get; set; }
            public string LName { get; set; }
            public string Gender { get; set; }
            public DateTime? DOB { get; set; }
            public string Email { get; set; }
            public DateTime DateLiked { get; set; }
            public string ImageURL { get; set; }
            //public string TestString { get; set; }
            //public string TestString2 { get; set; }
            public bool IsLiked { get; set; }
            //public string ImgUrl { get; set; }
    
            private Home()
            {
                //Console.WriteLine(GoogleAPIKey);
            }
    
            public static Home NotLiked()
            {
                Home h = new Home();
                h.IsLiked = false;
                return h;
            }
    
            public static Home Liked()
            {
                Home h = new Home();
                h.IsLiked = true;
                return h;
            }
    
            public void AddUser(string facebookID, string accessToken, string fName, string lName, DateTime dob, string email, DateTime dateLiked, string gender, string imageURL, string locationID, string locationValue, IEnumerable<FBInterests> interests)
            {
    
                Entities context = new Entities();
                User user = null;
                Location location = null;
    
                IEnumerable<User> users = context.Users.Where(u => u.FacebookID == facebookID);
    
    
                if (users.Count() == 0)
                {
                    //UserID = userID;
                    FBID = facebookID;
                    AccessToken = accessToken;
                    FName = fName;
                    LName = lName;
                    DOB = dob;
                    Email = email;
                    DateLiked = dateLiked;
                    Gender = gender;
                    ImageURL = imageURL;
    
    
                    User newUser = new User();
    
                    //newUser.UserID = 1;
                    newUser.FacebookID = facebookID;
                    newUser.AccessToken = accessToken;
                    newUser.FName = fName;
                    newUser.LName = lName;
                    newUser.Gender = gender;
                    newUser.DOB = DOB;
                    newUser.Email = email;
                    newUser.DateLiked = dateLiked;
                    newUser.ImageURL = imageURL;
    
                    context.Users.AddObject(newUser);
                    context.SaveChanges();
    
                    user = newUser;
                }
                else if (users.Count() == 1)
                {
                    User user0 = users.First();
    
                    if (user0.Gender != gender)
                    {
                        user0.Gender = gender;
                    }
    
                    if (user0.FName != fName)
                    {
                        user0.FName = fName;
                    }
    
                    if (user0.LName != lName)
                    {
                        user0.LName = lName;
                    }
    
                    if (user0.DOB != dob)
                    {
                        user0.DOB = dob;
                    }
    
                    if (user0.Email != email)
                    {
                        user0.Email = email;
                    }
    
                    if (user0.DateLiked != dateLiked)
                    {
                        user0.DateLiked = dateLiked;
                    }
    
                    if (user0.ImageURL != imageURL)
                    {
                        user0.ImageURL = imageURL;
                    }
    
                    context.SaveChanges();
    
                    FBID = user0.FacebookID;
                    AccessToken = user0.AccessToken;
                    FName = user0.FName;
                    LName = user0.LName;
                    DOB = user0.DOB;
                    Email = user0.Email;
                    DateLiked = user0.DateLiked;
                    Gender = user0.Gender;
                    ImageURL = user0.ImageURL;
    
                    user = user0;
                }
                else
                {
                    throw new ApplicationException();
                }
    
                //LOCATION...
    
                //LocationID = locationID;
                //FBID = facebookID;
                //Latitude = latitude;
                //Langitude = langitude;
                //Description = description;
    
                IEnumerable<Location> locations = context.Locations.Where(l => l.FacebookID == locationID);
    
                if (locations.Count() == 0)
                {
                    Location newLocation = new Location();
    
                    newLocation.FacebookID = locationID;
    
                    newLocation.Description = locationValue;
    
                    IGeoCoder geoCoder = new GoogleGeoCoder(GoogleAPIKey);
                    Address[] addresses = geoCoder.GeoCode(locationValue);
    
                    if (addresses.Length > 0)
                    {
                        // Let's assume the first one is good enough
                        Address address = addresses[0];
    
                        newLocation.Latitude = address.Coordinates.Latitude.ToString();
                        newLocation.Longitude = address.Coordinates.Longitude.ToString();
                        // Use location.Latitude and location.Longitude
    
                    }
    
    
                    context.Locations.AddObject(newLocation);
                    context.SaveChanges();
    
                    location = newLocation;
                }
                else if (locations.Count() == 1)
                {
                    location = locations.Single();
                }
                else
                {
                    throw new ApplicationException();
                }
    
                //Create a user location based on the user and location object, set the ordinal to the last (ordinal + 1)
                var userlocations = context.UserLocations.Where(ul => ul.UserID == user.UserID && ul.LocationID == location.LocationID);
                int ordinal;
    
                if (!userlocations.Any())
                {
                    ordinal = 0;
                    context.UserLocations.AddObject(new UserLocation() { UserID = user.UserID, LocationID = location.LocationID, Ordinal = ordinal });
                }
                else
                {
                    var latest = userlocations.OrderByDescending(ul => ul.Ordinal).First();
    
                    if (latest.LocationID != location.LocationID)
                    {
                        ordinal = latest.Ordinal + 1;
                        context.UserLocations.AddObject(new UserLocation() { UserID = user.UserID, LocationID = location.LocationID, Ordinal = ordinal });
                    }
                }
    
    
                context.SaveChanges();
    
                foreach (UserInterest ui in user.UserInterests)
                {
                    ui.Active = false;
                }
    
                foreach (var interest in interests)
                {
                    IEnumerable<Interest> ints = context.Interests.Where(i => i.FacebookID == interest.FBID);
                    //Add Interest if doesn't exist... :-)
    
                    Interest intrst;
    
                    if (ints.Count() == 0)
                    {
                        intrst = new Interest();
    
                        intrst.FacebookID = interest.FBID;
                        intrst.Category = interest.Category;
                        intrst.Value = interest.Name;
    
    
                        context.Interests.AddObject(intrst);
                    }
                    else if (ints.Count() == 1)
                    {
                        intrst = ints.Single();
                    }
                    else
                    {
                        intrst = null;
                        throw new ApplicationException("Duplicate Records Found");
                    }
    
                    //Fix FROM HERE
                    if (!user.UserInterests.Any(ui => ui.InterestsID == intrst.InterestsID))
                    {
                        //    Uintrst = new UserInterest();
    
                        //    Uintrst.InterestsID = UserInterest;
                        //    Uintrst.UserInterestID = UserInterest;
                        //    Uintrst.Active = UserInterest.Active;
    
                        //    context.UserInterests.AddObject(Uintrst);
                    }
                    else
                    {
                        //    Uintrst = new UserInterest.Single();
                        //    Uintrst = new UserInterest();
    
                        //    UInterest.Active = true;
                    }
                    //TO HERE...
                }
                context.SaveChanges();            
            }
    
    
            public string GoogleAPIKey
            {
    
                get
                {
                    return WebConfigurationManager.AppSettings["GoogleAPIKey"];
                }
    
            }
    
        }
    
    }
    
    
    Friday, September 30, 2011 6:10 AM

Answers

  • User3866881 posted

    want to update a field in my table to false. then do an if statement so if user doesnt exist in DB add userinterest with the active field set to true or else get userinterest and update active to true. 

    Hello,

    I think you can smimplify your statements like this:

    1) Try to find out the specific instance.

    2) If found, please change the property values again and call Update method.

    3) If not found, just create a new one and call AddTo method.

    sample codes are below:

    var a = user.UserInterests.Where(ui => ui.InterestsID == intrst.InterestsID).FirstOrDefault();
    if(a==null)
    {
         UserInterest u= new UserInterest();

                            u.InterestsID = UserInterest;
                            u.UserInterestID = UserInterest;
                            u.Active = UserInterest.Active;

                            context.UserInterests.AddObject(u);
    }
    else

    {

          a.Active = true;

    }

    user.SaveChanges();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 1, 2011 9:00 PM

All replies

  • User3866881 posted

    want to update a field in my table to false. then do an if statement so if user doesnt exist in DB add userinterest with the active field set to true or else get userinterest and update active to true. 

    Hello,

    I think you can smimplify your statements like this:

    1) Try to find out the specific instance.

    2) If found, please change the property values again and call Update method.

    3) If not found, just create a new one and call AddTo method.

    sample codes are below:

    var a = user.UserInterests.Where(ui => ui.InterestsID == intrst.InterestsID).FirstOrDefault();
    if(a==null)
    {
         UserInterest u= new UserInterest();

                            u.InterestsID = UserInterest;
                            u.UserInterestID = UserInterest;
                            u.Active = UserInterest.Active;

                            context.UserInterests.AddObject(u);
    }
    else

    {

          a.Active = true;

    }

    user.SaveChanges();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 1, 2011 9:00 PM
  • User-83227557 posted

    thanks following your example got me there in the end Smile

    Sunday, October 2, 2011 6:39 AM