I'm experiencing a really weird problem I have the following piece of code :
for (int i = 0; i < Board.Length - 2; i++)
{
var a = Board[i].Content;
var b = Board[i + 1].Content;
var c = Board[i + 2].Content;
if (a == b && a == c &&
(string) a != string.Empty && a != null)
{
MessageDialog msd = new MessageDialog("test");
await msd.ShowAsync();
}
}
Where Board is an array of buttons and a,b,c have the same value of "1". However when comparing them in the if statement they all give false ? The other statements where I check if the string is empty or null give value of true.
You are performing a reference equality comparison instead of a value equality comparison. Your code is equivelent to the following:
for (int i = 0; i < Board.Length - 2; i++)
{
object a = Board[i].Content;
object b = Board[i + 1].Content;
object c = Board[i + 2].Content;
if (a == b && a == c &&
(string) a != string.Empty && a != null)
{
MessageDialog msd = new MessageDialog("test");
await msd.ShowAsync();
}
}
This means that a == b is being resolved as <object> == <object> rather than <string> == <string>, which results in a comparison equivelent to Object.ReferenceEquals(a, b). To get value equality, you should immediately cast a, b and c. Now that a is a string, you can also use String.IsNullOrEmpty instead of manually checking both:
for (int i = 0; i < Board.Length - 2; i++)
{
string a = (string)Board[i].Content;
string b = (string)Board[i + 1].Content;
string c = (string)Board[i + 2].Content;
if (a == b && a == c && !String.IsNullOrEmpty(a))
{
MessageDialog msd = new MessageDialog("test");
await msd.ShowAsync();
}
}
Related
I'm using 3 iterators to iterate through an int[] object to find lists containing three int's that all sum to 0, without adding duplicate lists to the final return object. Once the iterators find a valid sequence, each array value that the iterator points to is added to a temporary IList object via syntax such as "tempList.Add(nums[a]);" e.t.c. This tempList is then added to the final IList<IList> return object.
To check for duplicates, I'm using the following sytax:
if ((nums[c] + nums[a] + nums[b] == 0) && !answerList.Contains(tempList))
{
answerList.Add(tempList);
}
This works when the IList<IList> is initially empty, however the second clause evaluated "false" for the following scenario, which is preventing the valid tempList from being added to the IList<IList> answerList :
IList<IList<int>> == [[-1,0,1]]
tempList == [-1,-1,2]
Do I need to extend a Comparable interface to correct this? I've double checked the array values for a, b and c and they collectively sum to 0, so I'm certain that it's the .Contains comparison that is causing issue.
Here is the code:
public IList<IList<int>> ThreeSum(int[] nums) // maybe use three index pointers to fill triplet, one starting at index 0, one
// starting at index nums.Lenght - 1 and one starting near or at middle
// 5 billion possible combinations of triplets among array of 3001 triplets
{
IList<int> tempList = new List<int>();
IList<IList<int>> answerList = new List<IList<int>>();
int countPositive = 0;
int countNegative = 0;
bool addAnswerList = false;
int indexPositive = 0;
int a = 0;
int b = 0;
int c = 0;
bool aExtreme = false;
bool bExtreme = false;
bool cExtreme = false;
if (nums == null)
return answerList;
if (nums.Length == 0 /* || /* nums.Length < 3 */)
return answerList;
if (nums.Length == 1 && nums[0] == 0)
return answerList;
for (int i = 0; i < nums.Length; i++) // corner case where all array values are either postive or negative
// 0 sum triplet not possible
{
if (nums[i] > 0)
countPositive++;
else if (nums[i] < 0)
countNegative++;
}
if ((countPositive == nums.Length || countNegative == nums.Length) || (countNegative == 0 && countPositive == 0))
return answerList;
if (nums.Length < 51)
{
int temp = 0;
for (int i = 1; i < nums.Length; i++) // use recursive sort to go through 3001 elements, use linear if less than 51
{
temp = nums[i];
int j = i;
while (j > 0 && nums[j-1] > temp)
{
nums[j] = nums[j-1];
j--;
}
nums[j] = temp;
}
Console.WriteLine(String.Join(" ", nums));
}
else if (nums.Length > 50) // recursive sort for larger input array
{
mergeSort(nums);
}
// Now nums is storted, 0 sum triplet will most likely be found where entries change from negative to positive
for (int i = 0; i < nums.Length; i++)
{
if (nums[i] > 0)
{
// toPositive = true;
indexPositive = i;
break;
}
}
Console.WriteLine(indexPositive);
if (indexPositive == 0) // case to make sure accessors do not get invalid number
{
a = 0; // initialize other two index accessors in addition to indexPositive
b = 0;
}
else
{
a = indexPositive - 1; // initialize other two index accessors in addition to indexPositive
b = indexPositive - 1;
}
c = indexPositive; // store initial indexPositive value into variable c as indexPositive will change
// throughtout the interation
// if indexPositive is less than (nums.Length / 2), iterate two other accessors towards end of nums
// else, interate two other accessors towards nums[0]
if (c < (nums.Length / 2)) // use iterator c in place of indexPositive
{
while (c < nums.Length && b < nums.Length && a >= 0)
{
if (a == 0 || a == nums.Length - 3)
{
aExtreme = true;
}
else
{
aExtreme = false;
}
if (b == 1 || b == nums.Length - 2)
{
bExtreme = true;
}
else
{
bExtreme = false;
}
if (c == 2 || c == nums.Length - 1)
{
cExtreme = true;
}
else
{
cExtreme = false;
}
if ((c != a && c != b && a != b) && (nums[c] + nums[a] + nums[b] == 0)) // if nums add to 0, add them to tempList
{
tempList.Add(nums[a]);
tempList.Add(nums[b]);
tempList.Add(nums[c]);
}
// need to check if exact triplet is duplicate or if tempList triplet contains same 3 values in different order
// 3 values in different order not an issues since input list is already sorted!!!!
// but need to be aware order accessors are added to temp list. Accessors cannot "crossover" one another!!!
if ((nums[c] + nums[a] + nums[b] == 0) && !answerList.Any(x => x.SequenceEqual(tempList)) /*!answerList.Contains(tempList)*/)
// Having issues, answerList.Contains does not currently do what is desired
{
answerList.Add(tempList); // add tempList to answerList
addAnswerList = true;
}
else
{
addAnswerList = false;
}
tempList.Clear(); // clear tempList to prepare for next entry
// Make changes here to make sure only 1 accessor iterates at a time, to check all possible and logical combinations
// Since array is now sorted, iteration towards sums[0] only has to continue if sum of 3 accessors is greater than 0
// Once active accessor returns a value that results in 0 sum, further iteration is guaranteed to return
// an identical triplet or one that sums to less than 0. At this point, the two accessors that are going in the same
// direction should be iterated by 1 position and the cycle repeated
if (nums[a] + nums[b] + nums[c] < 0) // still have chance to get to 0 value triplet with rightmost accessor
{
c++;
}
else if ((addAnswerList == true) || nums[a] + nums[b] + nums[c] > 0) // no point in further iteration,
// reset 'c' accessor to original starting
// position and decrement other accessors by 1
// towards first array element
{ // to begin cycle again
c = indexPositive - 1;
b--;
a--;
}
}
}
else // a and b accessors are interated towards 0
{
while (c < nums.Length && a >= 0 && b >= 0)
{
if (a == 0 || a == nums.Length - 3)
{
aExtreme = true;
}
else
{
aExtreme = false;
}
if (b == 1 || b == nums.Length - 2)
{
bExtreme = true;
}
else
{
bExtreme = false;
}
if (c == 2 || c == nums.Length - 1)
{
cExtreme = true;
}
else
{
cExtreme = false;
}
if ((c != a && c != b && a != b) && (nums[c] + nums[a] + nums[b] == 0)) // if nums add to 0, add them to tempList
// need to check if exact triplet is duplicate
{
tempList.Add(nums[a]);
tempList.Add(nums[b]);
tempList.Add(nums[c]);
}
Console.WriteLine(String.Join(" ", tempList));
// need to check if exact triplet is duplicate or if tempList triplet contains same 3 values in different order
// 3 values in different order not an issues since input list is already sorted!!!!
// but need to be aware order accessors are added to temp list. Accessors cannot "crossover" one another!!!
if ((nums[c] + nums[a] + nums[b] == 0) && !answerList.Any(x => x.SequenceEqual(tempList)))
// Having issues, answerList.Contains does not currently do what is desired
{
answerList.Add(tempList); // add tempList to answerList
addAnswerList=true;
}
else
{
addAnswerList = false;
}
tempList.Clear(); // clear tempList to prepare for next entry
// Make changes here to make sure only 1 accessor iterates at a time, to check all possible and logical combinations
// Since array is now sorted, iteration towards sums[0] only has to continue if sum of 3 accessors is greater than 0
// Once active accessor returns a value that results in 0 sum, further iteration is guaranteed to return
// an identical triplet or one that sums to less than 0. At this point, the two accessors that are going in the same
// direction should be iterated by 1 position and the cycle repeated
if (nums[a] + nums[b] + nums[c] > 0) // still have chance to get to 0 value triplet with
// leftmost accessor
{
a--;
}
else if ((addAnswerList == true) || nums[a] + nums[b] + nums[c] < 0) // no point in further iteration,
// reset 'a' accessor to original starting
// position and increment other accessors by 1
// towards last array element
{ // to begin cycle again
a = b; // a = indexPositive - 1;
if ((bExtreme == false && b < nums.Length - 2) && (cExtreme == false && c < nums.Length - 1))
// if there is still room to run towards end of array, increment, else decrement
{
b++;
c++;
}
else
{
bExtreme = true;
cExtreme = true;
a -= 2;
b--;
}
}
}
}
Console.WriteLine(String.Join(" ", answerList));
return answerList;
}
public void merge(int[] array1, int[] array2, int[] outArray)
{
int i = 0;
int j = 0;
while (i + j < outArray.Length)
{
if (j == array2.Length || (i < array1.Length && (array1[i] < array2[j])))
{
outArray[i + j] = array1[i++];
}
else
{
outArray[i + j] = array2[j++];
}
}
}
public void mergeSort(int[] inputArray)
{
if (inputArray.Length < 2)
return;
int[] firstHalf = new int[inputArray.Length / 2];
int[] secondHalf = new int[inputArray.Length / 2];
for (int i = 0; i < (inputArray.Length / 2); i++)
{
firstHalf[i] = inputArray[i];
}
for (int i = inputArray.Length / 2; i < inputArray.Length; i++)
{
secondHalf[i] = inputArray[i];
}
mergeSort(firstHalf);
mergeSort(secondHalf);
merge(firstHalf, secondHalf, inputArray);
}
You can replace
answerList.Contains(tempList))
which checks only for reference equality, with
answerList.Any(x => x.SequenceEqual(tempList))
which will run SequenceEqual on the answerList until it finds the first answer that has the same integer elements and returns false if none can be found.
Keep in mind that these functions require System.Linq.
Credit: thanks Hans Kesting for suggesting Any(...) over FirstOrDefault(...) != null
Is there any further optimization that I can do to the below LINQ query where you can see that I traded
HuffmanTable.SymbolObject[] objects = hTable.Symbols.Where(x => x.Bits == i + 1).ToArray();
for
var objects = hTable.Symbols.Where(x => x.Bits == i + 1);
I'm unsure what else I could do to speed up the Huffman code lookup.
It takes about 1.157 second to process a 10 scan progressive 707x707 JPEG in Release, and right at 4 seconds for a 1920x1080 jpeg. GetNextSymbol is the biggest bottleneck in the code.
HuffmanTable.SymbolObject GetNextSymbol(BitReader b, HuffmanTable hTable) {
b.LastPositionBeforeGetSymbol = b.Position;
uint currentCode = 0;
for (uint i = 0; i < 16; ++i) {
var c = b.ReadBit();
if (c == null)
{
return new HuffmanTable.SymbolObject()
{
Symbol = (int)currentCode,
Code = 0,
CodeString = null
};
//return null;
}
int bit = c.ValueByte;
if (bit == -1) {
return null;
}
currentCode = (uint)((currentCode << 1) | (uint)bit);
//HuffmanTable.SymbolObject[] objects = hTable.Symbols.Where(x => x.Bits == i + 1).ToArray();
var objects = hTable.Symbols.Where(x => x.Bits == i + 1);
int j = 0;
foreach(var obj in objects)
{
if (currentCode == obj.Code)
{
return obj;
}
}
}
Console.WriteLine("*** COULD NOT FIND CODE *** {0}:{1}", currentCode, Convert.ToString(currentCode, 2));
return null;
}
I am trying to create a program that will give me every single digit between 1 and 55 but 6 times in a row.
That is;
1-17-25-45-49-51
55-54-53-52-51-50
Note, two numbers can never repeat in a row.
I've tested this program on a smaller scale and so far it does as intended. However, if I run it full scale, as the code posted bellow shows, it runs into an "out of memory exception". My guess is, its because of the memory having to hold such big numbers while building the ArrayList and file consecutively.
How do I go about fixing this?
class MainClass
{
public static void Main (string[] args)
{
ArrayList newWords= new ArrayList();
System.Console.WriteLine ("Please enter word to prepend ");
int wordCount = 0;
string numbers;
string word = Console.ReadLine ();
string word2 = word;
string separator = "-";
System.Console.WriteLine ("Please enter location of where you would like the new text file to be saved: ");
string newFileDestination = Console.ReadLine ();
TextWriter writeToNewFile = new StreamWriter (newFileDestination);
for(int a = 1; a < 56; a++){
for(int b = 1; b < 56; b++){
for(int c = 1; c < 56; c++){
for(int d = 1; d < 56; d++){
for(int e = 1; e < 56; e++){
for(int f = 1; f < 56; f++){
if(a != b && a != c && a != c && a != e && a != f && b != c && b != d && b != e && b != f && c != d && c != e && c != f && d != e && d != f && e != f){
word = word2;
numbers = string.Concat(a, separator, b, separator, c, separator, d, separator, e, separator, f);
word += numbers;
newWords.Add (word);
}
}
}
}
}
}
}
foreach(string line in newWords){
writeToNewFile.WriteLine(line);
Console.WriteLine (line);
wordCount++;
}
writeToNewFile.Close ();
Console.WriteLine (wordCount);
Console.WriteLine ("Press any key to exit.");
System.Console.ReadKey ();
}
}
}
Without trying to fix the rest of your code, the basic principle you need to follow is to write out the numbers as you get them, instead of cumulating them in a list. You can get rid of the ArrayList altogether, and the accompanying foreach loop.
The relevant part would look like this:
TextWriter writeToNewFile = new StreamWriter (newFileDestination);
for(int a = 1; a < 56; a++){
for(int b = 1; b < 56; b++){
for(int c = 1; c < 56; c++){
for(int d = 1; d < 56; d++){
for(int e = 1; e < 56; e++){
for(int f = 1; f < 56; f++){
if(a != b && a != c && a != c && a != e && a != f && b != c && b != d && b != e && b != f && c != d && c != e && c != f && d != e && d != f && e != f){
word = word2;
numbers = string.Concat(a, separator, b, separator, c, separator, d, separator, e, separator, f);
word += numbers;
writeToNewFile.WriteLine(word);
Console.WriteLine (word);
wordCount++;
}
}
}
}
}
}
}
But, be warned. Your code will take a long time to complete. You just won't get the out of memory error. You may run out of hard disk space though :)
I got a list of files and directories List<string> pathes. Now I'd like to calculate the deepest common branch every path is sharing with each other.
We can assume that they all share a common path, but this is unknown in the beginning.
Let's say I have the following three entries:
C:/Hello/World/This/Is/An/Example/Bla.cs
C:/Hello/World/This/Is/Not/An/Example/
C:/Hello/Earth/Bla/Bla/Bla
This should get the result: C:/Hello/ as Earth is breaking this "chain" of subdirectories.
Second example:
C:/Hello/World/This/Is/An/Example/Bla.cs
C:/Hello/World/This/Is/Not/An/Example/
-> C:/Hello/World/This/Is/
How would you proceed? I tried to use string.split(#"/") and start with the first string and check if every part of this array is contained in the other strings. However, this would be a very expensive call as I'm iterating (list_of_entries)^list_of_entries. Is there any better solution available?
My current attempt would be something like the following (C# + LINQ):
public string CalculateCommonPath(IEnumerable<string> paths)
{
int minSlash = int.MaxValue;
string minPath = null;
foreach (var path in paths)
{
int splits = path.Split('\\').Count();
if (minSlash > splits)
{
minSlash = splits;
minPath = path;
}
}
if (minPath != null)
{
string[] splits = minPath.Split('\\');
for (int i = 0; i < minSlash; i++)
{
if (paths.Any(x => !x.StartsWith(splits[i])))
{
return i >= 0 ? splits.Take(i).ToString() : "";
}
}
}
return minPath;
}
A function to get the longest common prefix may look like this:
public static string GetLongestCommonPrefix(string[] s)
{
int k = s[0].Length;
for (int i = 1; i < s.Length; i++)
{
k = Math.Min(k, s[i].Length);
for (int j = 0; j < k; j++)
if (s[i][j] != s[0][j])
{
k = j;
break;
}
}
return s[0].Substring(0, k);
}
Then you may need to cut the prefix on the right hand. E.g. we want to return c:/dir instead of c:/dir/file for
c:/dir/file1
c:/dir/file2
You also may want to normalize the paths before processing. See Normalize directory names in C#.
I dont know whether this is the best performing solution (probably not), but it surely is very easy to implement.
Sort your list alphabetically
compare the first entry in that sorted list to the last in that list, character by character, and terminate when you find a difference (the value before the termination is the longest shared substring of both those strings)
Sample Fiddle
Sample code:
List<string> paths = new List<string>();
paths.Add(#"C:/Hello/World/This/Is/An/Example/Bla.cs");
paths.Add(#"C:/Hello/World/This/Is/Not/An/Example/");
paths.Add(#"C:/Hello/Earth/Bla/Bla/Bla");
List<string> sortedPaths = paths.OrderBy(s => s).ToList();
Console.WriteLine("Most common path here: {0}", sharedSubstring(sortedPaths[0], sortedPaths[sortedPaths.Count - 1]));
And that function of course:
public static string sharedSubstring(string string1, string string2)
{
string ret = string.Empty;
int index = 1;
while (string1.Substring(0, index) == string2.Substring(0, index))
{
ret = string1.Substring(0, index);
index++;
}
return ret;
} // returns an empty string if no common characters where found
First sort the list with the paths to inspect. Then you can split and compare the first and the last item - if they are same proceed to the next dimension until you find a difference.
So you just need to sort once and then inspect two items.
To return c:/dir for
c:/dir/file1
c:/dir/file2
I would code it this way:
public static string GetLongestCommonPrefix(params string[] s)
{
return GetLongestCommonPrefix((ICollection<string>)s);
}
public static string GetLongestCommonPrefix(ICollection<string> paths)
{
if (paths == null || paths.Count == 0)
return null;
if (paths.Count == 1)
return paths.First();
var allSplittedPaths = paths.Select(p => p.Split('\\')).ToList();
var min = allSplittedPaths.Min(a => a.Length);
var i = 0;
for (i = 0; i < min; i++)
{
var reference = allSplittedPaths[0][i];
if (allSplittedPaths.Any(a => !string.Equals(a[i], reference, StringComparison.OrdinalIgnoreCase)))
{
break;
}
}
return string.Join("\\", allSplittedPaths[0].Take(i));
}
And here are some tests for it:
[TestMethod]
public void GetLongestCommonPrefixTest()
{
var str1 = #"C:\dir\dir1\file1";
var str2 = #"C:\dir\dir1\file2";
var str3 = #"C:\dir\dir1\file3";
var str4 = #"C:\dir\dir2\file3";
var str5 = #"C:\dir\dir1\file1\file3";
var str6 = #"C:\dir\dir1\file1\file3";
var res = Utilities.GetLongestCommonPrefix(str1, str2, str3);
Assert.AreEqual(#"C:\dir\dir1", res);
var res2 = Utilities.GetLongestCommonPrefix(str1, str2, str3, str4);
Assert.AreEqual(#"C:\dir", res2);
var res3 = Utilities.GetLongestCommonPrefix(str1, str2, str3, str5);
Assert.AreEqual(#"C:\dir\dir1", res3);
var res4 = Utilities.GetLongestCommonPrefix(str5, str6);
Assert.AreEqual(#"C:\dir\dir1\file1\file3", res4);
var res5 = Utilities.GetLongestCommonPrefix(str5);
Assert.AreEqual(str5, res5);
var res6 = Utilities.GetLongestCommonPrefix();
Assert.AreEqual(null, res6);
var res7 = Utilities.GetLongestCommonPrefix(null);
Assert.AreEqual(null, res7);
}
I would iterate over each character in the first path, comparing it with every character in every path (except the first) in the collection of paths:
public string FindCommonPath(List<string> paths)
{
string firstPath = paths[0];
bool same = true;
int i = 0;
string commonPath = string.Empty;
while (same && i < firstPath.Length)
{
for (int p = 1; p < paths.Count && same; p++)
{
same = firstPath[i] == paths[p][i];
}
if (same)
{
commonPath += firstPath[i];
}
i++;
}
return commonPath;
}
You could iterate through the list first to find the shortest path and possibly improve it slightly.
The function that gives you the longest common directory path with best possible complexity:
private static string GetCommonPath(IEnumerable<string> files)
{
// O(N, L) = N*L; N - number of strings, L - string length
// if the first and last path from alphabetic order matches, all paths in between match
string first = null;//smallest string
string last = null;//largest string
var comparer = StringComparer.InvariantCultureIgnoreCase;
// find smallest and largest string:
foreach (var file in files.Where(p => !string.IsNullOrWhiteSpace(p)))
{
if (last == null || comparer.Compare(file, last) > 0)
{
last = file;
}
if (first == null || comparer.Compare(file, first) < 0)
{
first = file;
}
}
if (first == null)
{
// the list is empty
return string.Empty;
}
if (first.Length > last.Length)
{
// first should not be longer
var tmp = first;
first = last;
last = tmp;
}
// get minimal length
var count = first.Length;
var found = string.Empty;
const char dirChar = '\\';
var sb = new StringBuilder(count);
for (var idx = 0; idx < count; idx++)
{
var current = first[idx];
var x = char.ToLowerInvariant(current);
var y = char.ToLowerInvariant(last[idx]);
if (x != y)
{
// first and last string character is different - break
return found;
}
sb.Append(current);
if (current == dirChar)
{
// end of dir character
found = sb.ToString();
}
}
if (last.Length >= count && last[count] == dirChar)
{
// whole first is common root:
return first;
}
return found;
}
This is considerably more optimized than splitting paths by slash and comparing them:
private static string FindCommonPath(string[] paths) {
var firstPath = paths[0];
var commonPathLength = firstPath.Length;
for (int i = 1; i < paths.Length; i++)
{
var otherPath = paths[i];
var pos = -1;
var checkpoint = -1;
while (true)
{
pos++;
if (pos == commonPathLength)
{
if (pos == otherPath.Length
|| (pos < otherPath.Length
&& (otherPath[pos] == '/' || otherPath[pos] == '\\')))
{
checkpoint = pos;
}
break;
}
if (pos == otherPath.Length)
{
if (pos == commonPathLength
|| (pos < commonPathLength
&& (firstPath[pos] == '/' || firstPath[pos] == '\\')))
{
checkpoint = pos;
}
break;
}
if ((firstPath[pos] == '/' || firstPath[pos] == '\\')
&& (otherPath[pos] == '/' || otherPath[pos] == '\\'))
{
checkpoint = pos;
continue;
}
var a = char.ToLowerInvariant(firstPath[pos]);
var b = char.ToLowerInvariant(otherPath[pos]);
if (a != b)
break;
}
if (checkpoint == 0 && (firstPath[0] == '/' || firstPath[0] == '\\'))
commonPathLength = 1;
else commonPathLength = checkpoint;
if (commonPathLength == -1 || commonPathLength == 0)
return "";
}
return firstPath.Substring(0, commonPathLength);
}
for (int i = 0; i < dtblAllDB.Rows.Count; i++)
{
if ((table.Rows[i]["customer_id"].ToString() && table.Rows[i]["time"].ToString() ) != (dtblAllDB.Rows[i]["customer_id"].ToString() && dtblAllDB.Rows[i]["time"].ToString() ))
{
}
}
I need to compare two datatable column values, but I am getting the error,
here table and dtblAllDB are the datatables..
Operator '&&' cannot be applied to operands of type 'string' and 'string'
The && operand could be applied to bool but not string, your condition would be something like
for (int i = 0; i < dtblAllDB.Rows.Count; i++)
{
if (!(table.Rows[i]["customer_id"].ToString() == dtblAllDB.Rows[i]["customer_id"].ToString()
&& table.Rows[i]["time"].ToString() == && dtblAllDB.Rows[i]["time"].ToString()))
{
}
}
Although this does not make much sense as it will compare the tables row by row and filter out row which does not match both cutomer_id and time
If you want to compare two tables irrespective of row position then you need two loops like shown below.
for(int k = 0; k < table.Rows.Count; k++)
{
for (int i = 0; i < dtblAllDB.Rows.Count; i++)
{
if (!(table.Rows[k]["customer_id"].ToString() == dtblAllDB.Rows[i]["customer_id"].ToString()
&& table.Rows[k]["time"].ToString() == && dtblAllDB.Rows[i]["time"].ToString()))
{
}
}
}
ToString() returns a string that represents the current object. Use
Convert.ToString(table.Rows[i]["customer_id"])
in every case. And also use == to compare between strings
You need to compare every value seperatly. I think this is what you want to do:
if ((table.Rows[i]["customer_id"].ToString() != dtblAllDB.Rows[i]["customer_id"].ToString())
&& ((table.Rows[i]["time"].ToString() != dtblAllDB.Rows[i]["time"].ToString())
&& (table.Rows[i]["customer_id"].ToString() != dtblAllDB.Rows[i]["time"].ToString())
&& (table.Rows[i]["time"].ToString() != dtblAllDB.Rows[i]["customer_id"].ToString()))
Use plus sign(+) to concatenate two strings in c#
if ((table.Rows[i]["customer_id"].ToString() + table.Rows[i]["time"].ToString()) != (dtblAllDB.Rows[i]["customer_id"].ToString() + dtblAllDB.Rows[i]["time"].ToString()))
{ }
OR
if (table.Rows[i]["customer_id"].ToString() != dtblAllDB.Rows[i]["customer_id"].ToString() && table.Rows[i]["time"].ToString() != dtblAllDB.Rows[i]["time"].ToString())
{ }