Domanda

I have 2 ArrayList, which contains the filenames, now one list has more names and other might have less, or sometime equal, i want to list the filenames which are not common in both arraylist and accordingly update the database, i was able to check whether they are equal or not, but i am unable to list which list has extra elements and which are they.

Here is a code which i am currently using.

 ArrayList DatabaseSavedThumbnail = objSlideShow.GetAllThumbnailBySlideShowId();
        string[] FolderSavedThumbnail = Directory.GetFiles(Server.MapPath("~/Portals/2/SlideShowThumbnails/" + SlideShowName));

        if (Directory.GetFiles(Server.MapPath("~/Portals/2/SlideShowThumbnails/" + SlideShowName)).Length > 0)
        {
            if (!Object.Equals(DatabaseSavedThumbnail, FolderSavedThumbnail))
            {
                for (int i = 0; i < DatabaseSavedThumbnail.Count && i < FolderSavedThumbnail.Length; i++)
                {
                    if (!object.Equals(DatabaseSavedThumbnail[i], FolderSavedThumbnail[i]))
                    {
                        if (DatabaseSavedThumbnail.Count > FolderSavedThumbnail.Length)
                        {
                            objSlideShow.Thumbnail = "/Portals/2/SlideShowThumbnails/" + SlideShowName + "/" + Path.GetFileName(DatabaseSavedThumbnail[i].ToString());
                        }
                        else
                        {
                            objSlideShow.Thumbnail = "/Portals/2/SlideShowThumbnails/" + SlideShowName + "/" + Path.GetFileName(FolderSavedThumbnail[i].ToString());
                        }
                    }
                    Response.Write(objSlideShow.Thumbnail + "<br>");
                    /*objSlideShow.SlideTitle = String.Empty;
                    objSlideShow.SlideDescription = string.Empty;
                    objSlideShow.AddSlide();*/
                }
            }
        }

But this list all the elements of the arraylist which has more elements, i just want the differential elements, so that i can update the database with those elements only.

Can anyone tell me how could i get the differential records comparing 2 array list.

È stato utile?

Soluzione

Try this to get elements not common in both lists (assuming DatabaseSavedThumbnail has strings):

using System.Linq;
...
...
var dstArray = DatabaseSavedThumbnail.ToArray(typeof(string));
var fstArray = FolderSavedThumbnail;

var notCommonElements = dstArray.Union(fstArray).Except(dstArray.InterSect(fstArray));

A very naive iterative approach can be:

private IEnumerable<string> GetNotCommonElements(string[] array1, string[] array2)
{
    foreach (var item in array1)
    {
        if (!array2.Contains(item))
        {
            yield return item;
        }
    }

    foreach (var item in array2)
    {
        if (!array1.Contains(item))
        {
            yield return item;
        }
    }
}

Then use it like:

foreach(var item in GetNotCommonElements(dstArray, fstArray))
{
   // Do stuff with item
}

Altri suggerimenti

you can get idea from this

List<int> intersection = first.Cast<int>().Intersect(second.Cast<int>()).ToList();

or

ArrayList intersection = new ArrayList();
foreach (var i in first.Cast<int>().Intersect(second.Cast<int>()))
    intersection.Add(i);

More detail
Two ArrayList manipulation

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top