0

I have CreateDiscountViewByUser discountViewByUser it contains a list of cities that are chosen by the user, but they may already be those cities that have been added.

List<DiscountCity> discountCities = (from city in db.DiscountCities
                                 where city.DiscountId == discountViewByUser.Id
                                 select city).ToList();
            for (int y = 0; y < discountCities.Count(); y++)
            {
                var dc = discountCities[y];
                bool flag = false;
                for (int i = 0; i < discountViewByUser.DiscountCitys.Length; i++)
                {    
                    if (dc.CityId == discountViewByUser.DiscountCitys[i])
                    {
                        flag = true;
                        discountCities.Remove(dc);
                        y--;
                    }                      
                }
                if (!flag)
                {
                    db.DiscountCities.DeleteObject(dc);    
                }
            }
            foreach (var dc in discountCities)
            {
                DiscountCity discountCity = new DiscountCity
                                                {Id = Guid.NewGuid(),
                                                 CityId = dc.CityId,
                                                 DiscountId = main.Id};
                db.DiscountCities.AddObject(discountCity);
            }

how to add only the new city? My code does not work = (

UPDATE:

discountViewByUser.DiscountCitys type int[].

db.DiscountCities table: Id DiscountId CityId.

example: in database: Odessa, Kiev

user set: Odessa, Moscow.

I need delete Kiev and add moscow how do this?

Chris
  • 6,272
  • 9
  • 35
  • 57
den den
  • 469
  • 2
  • 6
  • 9
  • There are a few things about your code that makes no sense. 1) what is `main.Id` - its not refered to anywhere else. 2) you have a partial line in there `discountViewByUser.DiscountCitys.`. Can you describe what it is that you're trying to do, rather than how you're trying to achieve it? – Jamiec Mar 13 '12 at 11:38
  • Can you explain what is really your requirement? your description seems to be confusing, we can not help until we understand your problem – Jayanga Mar 13 '12 at 11:41

2 Answers2

1

What I recommend is adding all the items and then removing duplicates.

// Where uniqueList is a List<T> of unique items:
uniqueList.AddRange(valuesToAdd);
uniqueList = uniqueList.Distinct(new CityEqualityComparer()).ToList();
// Sorry, I don't know how this would fit into your code

Since you are comparing cities by their CityId's, you will probably need to use a custom IEqualityComparer to determine which cities are duplicates.

Here is an example of such a class:

class CityEqualityComparer : IEqualityComparer<City>
{
    public bool Equals(City arg1, City arg2)
    {
        return arg1.CityId == arg2.CityId;
    }

    public int GetHashCode(City arg)
    {
        return arg.CityId;
    }
}

This question may also be of some help.

Community
  • 1
  • 1
Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
0

I suggest you do this in 2 steps.

1) Find the cities to be deleted

var deleteCities = db.DiscountCities.Where(c => c.DiscountId == discountViewByUser.Id
        && !discountViewByUser.DiscountCitys.Contains(c.CityId));
foreach(deleteCity in deleteCities)
{
   db.DiscountCities.DeleteObject(deleteCity);
}

2) Find cityId's to be inserted

var insertCities = discountViewByUser.DiscountCitys.Except(
           db.DiscountCities.Where(c => c.DiscountId == discountViewByUser.Id)
                            .Select(c => c.CityId));
foreach(var insertCity in insertCities)
{
    DiscountCity discountCity = new DiscountCity
           {Id = Guid.NewGuid(), CityId = insertCity, DiscountId = discountViewByUser.Id};
    db.DiscountCities.AddObject(discountCity);    
}
Jamiec
  • 133,658
  • 13
  • 134
  • 193