-from the list an example is 9120038560640 occurs twice or could be more than that.
-lines stored in List<.string> items = File.ReadAllLines(filepath).ToList();
-every line is split in semi colon.
-second index or [1] should compare to all of the lines and remove with found matched.
363193;9120038560640;7,11;9,99 <---- must be remove
363195;9120038560641;9,81;14,99
363194;9120038560640;9,81;14,99 <--- must be remove
363196;9120038560642;9,81;14,99
363197;9120038560643;9,81;14,99
....
..
.
btw. my file has 25,000++ items.
thank you
okay i got the answer and this is working
items = items.Where(x => x.Split(';')[columnIndex] != "").OrderBy(x => x.Split(';')[columnIndex]).ToList();
List<.string> _items = items.ConvertAll(z => z); //I make independent copy
string[] itemsArr = items.ToArray();
int countA = 0;
foreach (string itemArr in itemsArr)
{
List<int> groupDuplicates = new List<int>();
for (int a = countA; a < itemsArr.Count(); a++)
{
if (itemArr != itemsArr[a])
{
if (itemArr.Split(';')[columnIndex] == itemsArr[a].Split(';')[columnIndex]) //if matched then add
{
groupDuplicates.Add(a); // listing index to be remove
}
else
break; //no way to go through the bottom of the list and also to make the performance faster
}
countA++;
}
if (groupDuplicates.Count() != 0)
{
groupDuplicates.Add(groupDuplicates.First() - 1); //I add here the first item in duplicates
foreach (int m in groupDuplicates)
{
_items.Remove(items.ElementAt(m)); //remove by item not by index
}
}
}
Related
I have 2 GameObject and each other has a lot of child. This two GameObject same as same except some child's exist or child's position. I found each child's name.
And also I found locations info for each item. Then I compared this Lists and i found relocated, exist, added and removed items. I want to match with relocated items with names. How can I do that?
On Project1
List<string> existItems = GetNames().Intersect(revisedBasicProject4.GetNames4()).ToList();
List<Vector3> comparePosition = GetLocations().Except(revisedBasicProject4.GetLocations()).ToList();
foreach (var item in existItems)
{
isExist = true;
}
if (isExist)
{
foreach (var item in comparePosition)
{
Debug.Log("RELOCATED ITEMS :" +item);
}
}
I do not know if I understood what you meant correctly or not, but you can use the following code
List<string> existItems = GetNames().Intersect(revisedBasicProject4.GetNames4()).ToList();
List<Vector3> comparePosition = GetLocations().Except(revisedBasicProject4.GetLocations()).ToList();
foreach (var item in existItems)
foreach (var li in comparePosition)
if (item.(...) == li.(...)) {
Debug.Log("RELOCATED ITEMS :" + item);
}
Following on from https://stackoverflow.com/users/7111561/derhugo and https://stackoverflow.com/users/13922490/mohammad-asadi - You want to store names and locations together, so use Transform:
Then, you can compare your revisedBasicProject4 Transforms with the current class like so:
foreach(var transform in GetTransforms())
{
foreach(var revisedBasicProject4Transform in revisedBasicProject4.GetTransforms())
{
if ( transform.name == revisedBasicProject4Transform.name
&& transform.position != revisedBasicProject4Transform.position)
{
Debug.Log("RELOCATED ITEM : " + transform.name);
}
}
}
If you find this is taking forever (because you have millions of Transforms, you may be able to optimise this by doing the following:
var transforms = new List<Transform>(GetTransforms());
var otherTransforms= new List<Transform>(revisedBasicProject4.GetTransforms());
for(var i = 0; i < transforms.Count; i++)
{
for(var j = 0; j < otherTransforms.Count; j++)
{
if ( transforms[i].name == otherTransforms[j].name )
{
if ( transforms[i].position != otherTransforms[j].position )
Debug.Log("RELOCATED ITEM : " + transforms[i].name);
otherTransforms.RemoveAt(j); //We found it so remove it from our temporary "otherTransforms" list
break; //We found it so move on to the next "transforms" item
}
}
}
I have list of 10 Iwebelements . i've stored their text into another list(As i just need to check that all 10 are in Alphabetical Order)
Can any one please tell me how to do that .
(Below code will get the list of all elements and with Foreach i'm getting their text and adding them into X list)
IList<IWebElement> otherSports = Driver.FindElements(By.CssSelector(".sports-buttons-container .other-sport .sport-name"));
List<string> x = new List<string>();
foreach (var item in otherSports)
{
string btnText = item.Text.Replace(System.Environment.NewLine, "");
x.Add(item.Text.Replace(System.Environment.NewLine, ""));
}
Note:- I dont want to sort the list . I just want to see if all items in List X is in Alphabetical order.
Any Help would be appreciated.
You can use StringComparer.Ordinal to check if two strings are in alphabetical order.
var alphabetical = true;
for (int i = 0; i < x.Count - 1; i++)
{
if (StringComparer.Ordinal.Compare(x[i], x[i + 1]) > 0)
{
alphabetical = false;
break;
}
}
An alternative solution if you prefer LINQ(but less readable, IMO)
var alphabetical = !x.Where((s, n) => n < x.Count - 1 && StringComparer.Ordinal.Compare(x[n], x[n + 1]) > 0).Any();
EDIT since you are generating the list your self, you can add solution 1 into your code when the list is created, that's more efficient.
IList<IWebElement> otherSports = Driver.FindElements(By.CssSelector(".sports-buttons-container .other-sport .sport-name"));
List<string> x = new List<string>();
var alphabetical = true;
string previous = null;
foreach (var item in otherSports)
{
string btnText = item.Text.Replace(System.Environment.NewLine, "");
var current = item.Text.Replace(System.Environment.NewLine, "");
x.Add(current);
if (previous != null && StringComparer.Ordinal.Compare(previous,current) > 0)
{
alphabetical = false;
}
previous = current;
}
I would do it like that
[TestMethod]
public void TestOrder()
{
IList<IWebElement> otherSports = Driver.FindElements(By.CssSelector(".sports-buttons-container .other-sport .sport-name"));
var x = otherSports.Select(item=>item.Text.Replace(System.Environment.NewLine, ""))
var sorted = new List<string>();
sorted.AddRange(x.OrderBy(o=>o));
Assert.IsTrue(x.SequenceEqual(sorted));
}
So this method will fail untill list x list is ordered alphabetically
I want to remove multiple row from datagridview,
I tried the below code, here row's are getting deleted based on index.
for (int m = 0; m < dataGridView3.Rows.Count - 1; m++)
{
if (dataGridView3.Rows[m].Cells[2].Value != null)
{
for (int n = 0; n < dataGridView2.Rows.Count - 1; n++)
{
if (dataGridView2.Rows[n].Cells[2].Value != null)
{
if (dataGridView2.Rows[n].Cells[2].Value.Equals(dataGridView3.Rows[m].Cells[2].Value) &&
dataGridView2.Rows[n].Cells[8].Value.Equals(dataGridView3.Rows[m].Cells[8].Value))
{
dataGridView2.Rows.RemoveAt(n);
//break;
}
}
}
}
}
here rows are not getting deleted properly, because index is changed after every single delete, so some records are missing out from loop.
can anyone help me how to solve this?
If you're going to remove items from the collection as you iterate through it like this, you'll need to work backwards through the collection of rows:
// start with the last row, and work towards the first
for (int n = dataGridView2.Rows.Count - 1; n >= 0; n--)
{
if (dataGridView2.Rows[n].Cells[2].Value != null)
{
if (dataGridView2.Rows[n].Cells[2].Value.Equals(dataGridView3.Rows[m].Cells[2].Value) &&
dataGridView2.Rows[n].Cells[8].Value.Equals(dataGridView3.Rows[m].Cells[8].Value))
{
dataGridView2.Rows.RemoveAt(n);
//break;
}
}
}
Alternatively, you could use LINQ to find your matches first, and then remove them:
var rowToMatch = dataGridView3.Rows[m];
var matches =
dataGridView2.Rows.Cast<DataGridViewRow>()
.Where(row => row.Cells[2].Value.Equals(rowToMatch.Cells[2].Value)
&& row.Cells[8].Value.Equals(rowToMatch.Cells[8].Value))
.ToList();
foreach (var match in matches)
dataGridView2.Rows.Remove(match);
Just to make it less of a maintenance head-ache, you might want to use the column name instead of the column index too... just a thought.
I have an array as
That is, each item has its category in the following index.
I need all the items whose category are TotalNumbers and CurrentNumbers.
I tried
int i = 1;
foreach (string item in statsname)
{
//only number type stats are added to the comboboxes.
if ((statsname[i].ToUpperInvariant()==("TOTALNUMBER")) || ((statsname[i].ToUpperInvariant()==("CURRENTNUMBER"))))
{
comboBox1.Items.Add(statsname[i-1]);
i++;
i++;
}
comboBox1.SelectedIndex = 0;
}
Apparently this does not checks for what I need correctly.
How do I need to modify my codes to get what i need ?
Seems it's better to use a for loop instead of foreach:
for (int i = 1; i < statsname.Length; i += 2)
{
//only number type stats are added to the comboboxes.
if ((statsname[i].ToUpperInvariant()==("TOTALNUMBER")) || ((statsname[i].ToUpperInvariant()==("CURRENTNUMBER"))))
comboBox1.Items.Add(statsname[i-1]);
}
Linq comes to rescue!
var listItems = from s in statsname where s.Equals("TOTALNUMBER", StringComparison.InvariantCultureIgnoreCase) || s.Equals("CURRENTNUMBER", StringComparison.InvariantCultureIgnoreCase) select new ListItem(s);
comboBox1.AddRange(listItems);
Code not tested or compiled, but you can have an idea of what i said.
var filteredValues = Array.FindAll(source, s => s.ToUpperInvariant() == "TOTALNUMBER" ||
s.ToUpperInvariant() == "CURRENTNUMBER").ToList()
I am not sure why you are using index in an foreach loop. The below code should work for you
foreach (string item in statsname)
{
if ( item.ToUpper() == "TOTALNUMBER" || item.ToUpper() == "CURRENTNUMBER")
{
comboBox1.Items.Add(item);
}
}
comboBox1.SelectedIndex = 0;
How would you check multiple RichTextboxes containing multiple lines for unique and or duplicate lines. I've figured out how to loop through the richTextboxes and get the data, but I'm struggling on the logic to see if it's unique. My apologies if code is as clear as mud.
List<string> DistinctItems = new List<string>();
List<string> DupilcatedItems = new List<string>();
List<string> FirstItemsList = new List<string>();
List<string> CompareItemsList = new List<string>();
int ElementIndex = 0;
foreach (RichTextBox c in tableLayoutPanel1.Controls)
{
if (c.Text != null)
{
FirstItemsList.Add(c.Text.Replace("\n", Environment.NewLine).ToString());
if (CompareItemsList.Count == 0)
{
//Have to add the first batch
foreach (string str in FirstItemsList)
{
txtMixerTextBox.AppendText(str);
txtDistinctItems.AppendText(str);
DistinctItems.Add(str);
ElementIndex++;
}
CompareItemsList.Add(c.Text.Replace("\n", Environment.NewLine).ToString());
if (CompareItemsList.Count() > 0)
{
//OK we've gone through the first set
foreach (string s in CompareItemsList)
{
if (DistinctItems.Contains(s))
{
//It's a duplicate see if it's in the duplicate list
if (DupilcatedItems.Contains(s))
{
//OK it's in the list we don't have to add it
//See if it's in the textbox
if (!txtDuplicateItems.Text.Contains(s))
{
//OK it's not in the textbox let's add it
txtDuplicateItems.AppendText(s);
}
}
}
else
{
//It's not there so add it
DupilcatedItems.Add(s);
//now see if it's in the Distinct Textbox
if (!txtDistinctItems.Text.Contains(s))
{
//add it
txtDistinctItems.AppendText(s);
}
txtMixerTextBox.AppendText(s);
}
}
}
}
}
}
Use String.Split.
For example:
foreach (RichTextBox c in tableLayoutPanel1.Controls)
{
if (!c.Text.IsNullOrWhiteSpace)
{
string[] lines = c.Text.Split('\n');
string[] uniqueLines = GetUniqueLines(lines);//Some line-uniqueness checking algorithm
c.Text = String.Join('\n',uniqueLines)
}
}
This is what I did to get the results I was after. Looping through the RichTextboxes as noted above, I wrote the list to a file, stripped out the blank lines (where they came from I haven't the foggiest), read the file into a new list and then got the distinct list from there. I think the blank lines may have been messing me up, or it might have been the fact that I was looping through the strings in the list (again) thus giving myself duplicates. I'll likely get hammered for it, but it worked for me.
List<string> SortingList = new List<string>();
using (StreamReader r = new StreamReader("DistinctItemsNoBlankLines.txt"))
{
string line;
while ((line = r.ReadLine()) != null)
{
SortingList.Add(line);
}
}
List<string>DistinctSortingList = SortingList.Distinct().ToList();
foreach (string str in DistinctSortingList)
{
int index = 0;
while ( index < DistinctSortingList.Count() -1)
{
if (DistinctSortingList[index] == DistinctSortingList[index + 1])
DistinctSortingList.RemoveAt(index);
else
index++;
}
}
txtDistinctItems.Lines = DistinctSortingList.ToArray();