I have two arrays say
var list1 = string[] {"1", "2", "3", "4", "", ""};
var list2 = string[] {"2", "3", "4","",""};
When i try to get common items form these two array using following code
var listCommon = list1.Intersect(list2);
It gives me result like this
string[] {"2", "3", "4", ""}
But i want it should return like this
string[] {"2", "3", "4", "", ""}
It is escaping last empty string value while intersecting.
Set methods like Intersect or Except remove duplicates from each collection. I assume you want something like this instead:
var listCommon = list1.Where(list2.Contains);
which is not as efficient. This could be an optimization:
var l2Lookup = new HashSet<string>(list2);
var listCommon = list1.Where(l2Lookup.Contains);
This will work:
list1.Where(x=>list2.Contains(x))
Related
I have two lists that I believe to be equal and in same sequence, but when I run SequenceEqual() it is returning false. See below for pseudo example:
// For brevity’s sake assume the two-list data is as follows
List<string> list1 = new List<string> {"1", "2", "3"};
List<string> list2 = new List<string> {"1", "2", "3"};
list1.SequenceEqual(list2); // returning false
list1.Except(list2).Count(); // returning 0
list2.Except(list1).Count(); // returning 0
In reality my list data is much larger (~ 8000 items), but I am confused why I would get 0 for both Except().Count() yet false for SequenceEqual()?
Edit: Added Count to the Except examples.
You do different comparisons. As for Except it
Removes duplicates
Doesn't take order of items into account
Ignores items which are in the second argument if they are not present in the first
that's why
// Duplicated "2" will be ignored
List<string> list1 = new List<string>() { "1", "2", "3", "2" };
// Duplicated "3" will be ignored
// Order doesn't matter
// "7777777" will be ignored
List<string> list2 = new List<string>() { "3", "3", "1", "2", "7777777"};
// 0; since all items within list1 - "1", "2", "3"
// are in the list2, duplicates ignored
int count = list1.Except(list2).Count();
When SequenceEqual returns true if and only if sequences are equal order and duplicates matter. In the example above SequenceEqual returns false since list1.Count != list2.Count, list1[0] != list2[0] etc.
I have following code to find where clause to search rows which contains numeric on first character of column
if you have solutions please reply. below code result but only single row is returning
var strs = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
query = query.Where("#0.Contains(Name.Substring(0,1))", strs);
query = query.Where("Name.Substring(0,1).Contains(#0)", strs);
query = query.Where("Name.Contains(#0)", strs);
query.Where("Name.StartsWith(\"[0-9]\")");
You can use the following:
query = query.Where("#0.Contains(outerIt.Name.Substring(0, 1))", (object)strs);
There are two Dynamic LINQ specifics.
First, the last argument of most of the dynamic methods is declared as params object[] values, so in order to pass the string array as single variable, you need to cast it to object, otherwise since string[] is castable to object[], it will be passed as variables and the #0 will basically refer to strs[0].
Second, once you pass the strs correctly, for some reason you have to use outerIt inside the Contains method to access the entity property.
P.S. Another way is to get rid of the strs array and use the following criteria:
query = query.Where("Name.Substring(0, 1) >= \"0\" and Name.Substring(0, 1) <= \"9\"");
I have a little problem converting my JSON Object I have to something I can work within C# code.
{ "CheckboxHours": {
"method": "ID",
"valueparts": [
"Hour",
[ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" ]
]
}
}
This is my JSON String I have, which is for creates me the Selenium Object.
What i want is to combine the values from valueparts[0] ("Hour") with valueparts[1][0-23] with each other and create an array of Selenium Objects
var jsonData = JsonConvert.DeserializeObject<jsonc#file>(_jsonFile);
Object[] hours = (Object[])jsonData.CheckboxHours.Valueparts[1];
SeleniumCheckBox[] checkbox = new SeleniumCheckBox[hours.Length];
for (int i = 0; i < hours.Length; ++i)
{
cbHour[i] = new SeleniumCheckBox(jsonData.CheckboxHours.Valueparts[0].ToString() + hours[i]);
}
The SeleniumCheckBox is a Class I made which just takes the value and creates in the background a new Selenium Element with findElement(By.ID(value)). This is working already.
My Problem here is that he doesn't allow the Conversion from jsonData to Object[] and I don't really know how I can handle this.
I hope it is clear what I want to have - if not feel free to ask for more specific data.
I think. You can try this.
var jsonData = JsonConvert.DeserializeObject<dynamic>(_jsonFile);
var hours = jsonData.CheckboxHours.valueparts[1];
foreach (var hour in hours)
{
//Some code
}
I am not sure if I understood everything correctly, but the reason for failure in deserialization might be that valueparts is List parameter, usually in c# the Lists contain only 1 type of parameters eg. string, or int. In your case, "Hour" (valueparts[0]) is a string, and valueparts[1] is List. I assume here is the conflict with deserializing the JSON string.
{
CheckboxHours:{
"method":"ID",
"valueparts":{
Measure: "Hour",
MesureValues:
[
"0","1","2","3","4","5","6","7",
"8","9","10","11","12","13","14",
"15","16","17","18","19","20",
"21","22","23"
]
}
}
}
Change valueparts to object, as in the code above, so you can use Hour as string and the values as List.
i have string array as the following
string[] strArray = new string[] { "1", "2", "3", "5", "6" };
the question is
how can i add item to this created Array at specific position
to be like
{ "1", "2", "3" , "4" , "5", "6" }
i need some thing can append value at specific index and keep the old values
You can't do this, since arrays have fixed length after creation. Use List<string> instead (which internally stores its items in array):
var strList = new List<string> { "1", "2", "3", "5", "6" };
strList.Insert(2, "foo");
If you'll need to convert the list into array, use ToArray extension method:
var strArray = strList.ToArray();
Arrays are immutable, but Lists are not:
var list = strArray.ToList();
list.Add("6")
strArray = list.ToArray();
Or, at a specific index:
var list = strArray.ToList();
list.Insert(3, "4");
strArray = list.ToArray();
i have a loop where a variable value will change in each loop and display those values in each loop. i need to skip the display of the value if the same value repeats from second time
You need to remove the duplicates from the list. Ive chosen StringCollection from the System.Collections.Specialized namespace. But you could use List from System.Collections.Generic
String[] strings = new String[] { "1", "2", "3", "4", "2", "5", "4", "6", "7" };
StringCollection unique = new StringCollection();
foreach (String s in strings)
{
if (!unique.Contains(s))
unique.Add(s);
}
foreach (String s in unique)
{
Console.WriteLine(s);
}
in speudo code
declare var1
for each item in the collection
check if item is var1, if not print item
set item to var1