Wednesday, September 18, 2013

How can i make and calculate a new List that will have the items minus the number of items from another List ?

@What i want to do is that result will contain the same files


Thought:Fetch the file names whose suffix (6 digits) different from each other and combine into one array by referring Except method.


Solution by net framework 3.5 or above:



using System;
using System.Collections.Generic;
using System.Linq;

namespace CSharp
{
class Program
{
private class DigitCompare : IEqualityComparer<string>
{
public bool Equals(string x, string y)
{
return x.Substring(4) == y.Substring(4);
}

public int GetHashCode(string obj)
{
return obj.Substring(4).GetHashCode();
}
}
/// <summary>
/// Fetch the rest files by comparing the suffix digits
/// </summary>
/// <param name="file">Original file names</param>
/// <param name="file1">File names to be compared with</param>
/// <returns>The rest file names</returns>
static List<string> GetRestFiles(List<string> file, List<string> file1)
{
return file.Except(file1,new DigitCompare()).ToList();
}

static void Main()
{
//Generate for List
List<string> file = new List<string>();
for (int i = 0; i < 61; i++)
{
file.Add(string.Format("file{0:000000}", i));
}

//Generate for List1
List<string> file1 = new List<string>();
for (int i = 0; i < 9; i++)
{
file1.Add(string.Format("test{0:000000}", i));
}

//Output result
foreach (var item in GetRestFiles(file,file1))
{
Console.WriteLine(item);
}
}
}
}



If you think one reply solves your problem, please mark it as An Answer , if you think someone's reply helps you, please mark it as a Proposed Answer



Help by clicking:

Click here to donate your rice to the poor

Click to Donate

Click to feed Dogs & Cats




Found any spamming-senders? Please report at: Spam Report


No comments:

Post a Comment