I have an array of string :
string[] PropertyIds= new string[5];
A List of Class(Property)
List<Property> properties = new List<Property>();
The class Property has following fields:
PropertyId (string) and PropertyDesc (string)
I have to find all the values of PropertyId in array PropertyIds, which are not in List properties.
e.g.
string[] PropertyIds= new string[] { "one", "two", "three" };
List<Property> properties = new List<Property>()
{
new Property("one","This is p1"),
new Property("Five","This is p5"),
new Property("six","This is p6"),
};
Then my result should be two and three.
Use Enumerable.Except to get difference from two sequences:
var result = PropertyIds.Except(properties.Select(p => p.PropertyId));
Related
I have an <int, string> dictionary and I am trying to check an IEnumerable of objects to see, whether a particular attribute is contained in the dictionary. I am aware of Contains but I can't seem to reverse it if it makes sense.
If it were just one value I'd be fine but I need the list to go through the whole dictionary for each item in the list.
Currently I am using this:
foreach (var item in model)
{
if (dictionary.Values.Contains(object.Attribute))
{
list.Add(object);
}
}
Thank you!
Take a look at the code below, and if you want to play with it use this Fiddle. This takes Jon Skeet's advice to use a HashSet.
// your dictionary of items
var dict = new Dictionary<int, string>();
dict.Add(0, "Zero");
dict.Add(2, "Two");
dict.Add(4, "Four");
// as suggested by Jon Skeet, create a HashSet to be more performant
var hs = new HashSet<string>(dict.Values);
// list of unfiltered items
var list = new List<dynamic>()
{
new { Id = 0, Name = "Zeroth", PropertyYouWantToCheck = "Zero" },
new { Id = 1, Name = "First", PropertyYouWantToCheck = "One" },
new { Id = 2, Name = "Second", PropertyYouWantToCheck = "Two" },
new { Id = 3, Name = "Third", PropertyYouWantToCheck = "Three" },
new { Id = 4, Name = "Fourth", PropertyYouWantToCheck = "Four" },
};
// LINQ query to filter the list
var filteredList = list.Where(i => hs.Contains(i.PropertyYouWantToCheck));
// output the name of the filtered items
Console.WriteLine(string.Join(", ", filteredList.Select(fl => fl.Name)));
I'am comparing 2 List<string> wether the first list does contain an string that is not on the second List
This code works fine:
var onlyInXML = xmlList[i].columns.Except(rowAndTables[xmlList[i].table]);
if (onlyInXML.Any()) {
//Console.Write the not matching item here
}
I want to get the string, which is not matching. how do i do that ?
This way:
List<string> listOfStrings1 = new List<string>() { "abc", "def", "ghi", "lmn" };
List<string> listOfStrings2 = new List<string>() { "abc", "def", "lmn" };
List<string> listOfNotContainedStrings = listOfStrings1.Where(x => listOfStrings2.Contains(x) == false).ToList();
I am new to C# and I am trying to define a Dictionary having:
as key:
a string
as value:
a List of Lists of strings.
What I could came up (not entirely sure it's right?) is this:
var peopleWithManyAddresses = new Dictionary<string, List<List<string>>> {};
Now, if the above is right, I would like to know how to populate one item of peopleWithManyAddresses.
Intellisense is telling me that the following is correct only up until "Lucas":
peopleWithManyAddresses.Add("Lucas", { {"first", "address"}, {"second", "address"} });
What's the correct syntax for it?
P.S. I know I could use a class, but for learning purposes I'd like to do it this way for now.
To initialize the List<List<string>> object, you have to use the new List<List<string>> { ... } syntax. And to initialize each sub list you have to use a similar syntax, i.e. new List<string> {... }. Here is an example:
var peopleWithManyAddresses = new Dictionary<string, List<List<string>>>();
peopleWithManyAddresses.Add("Lucas", new List<List<string>>
{
new List<string> { "first", "address" },
new List<string> { "second", "address" }
});
Your initialization statement is correct.
Using C# 6.0, you can use the following syntax to populate one item:
var dict = new Dictionary<string, List<List<string>>>
{
["Lucas"] = new[]
{
new[] { "first", "address" }.ToList(),
new[] { "second", "address" }.ToList(),
}.ToList()
};
You could use the following to populate two items:
var dict = new Dictionary<string, List<List<string>>>
{
["Lucas"] = new[]
{
new[] { "first", "address" }.ToList(),
new[] { "second", "address" }.ToList(),
}.ToList(),
["Dan"] = new[]
{
new[] { "third", "phone" }.ToList(),
new[] { "fourth", "phene" }.ToList(),
}.ToList(),
};
If you want to add more data later, you can do the following:
dict["Bob"] = new[]
{
new[] { "fifth", "mailing" }.ToList(),
new[] { "sixth", "mailing" }.ToList(),
}.ToList();
first I create List separated from Dictionary:
List<string> someList = new List<string<();
var otherList = new List<List<string>>();
var peopleWithManyAddresses = new Dictionary<string, List<List<string>>> {};
First add strings in someList
someList.Add("first");
someList.Add("addresss");
Then add in otherList:
otherList.Add(someList);
Now create new List of strings:
var thirdList = new List<string>();
thirdList.Add("second");
thirdList.Add("addresss");
And add the last list of strings in other list and add in dictionary
otherList.Add(thirdList);
peopleWithManyAddresses.Add("Lucas", otherList);
How do you add a array of strings to a List?
string csv = "one,two,three";
string[] parts = csv.Split(',');
_MyList.Add(new ListObjects()
{
Name = tag.Name,
MyObjectList = new List<string>(new string[] { parts })
});
This works:
_MyList.Add(new ListObjects()
{
Name = tag.Name,
MyObjectList = new List<string>(new string[] { "one", "two", "three" })
});
But then this is hardcoded. Is it even possible to split a string by "," and then add those values to a List
Use the ToList() method to convert the Array to a List.
string csv = "one,two,three";
string[] parts = csv.Split(',');
_MyList.Add(new ListObjects()
{
Name = tag.Name,
MyObjectList = parts.ToList()
});
Well, parts is an array already, just pass it to the List's constructor:
string csv = "one,two,three";
string[] parts = csv.Split(',');
_MyList.Add(new ListObjects()
{
Name = tag.Name,
MyObjectList = new List<string>(parts)
});
You can just use ToList<TSource>() method to do this:
var List = csv.Split(',').ToList();
The easiest thing to do is simply to use string.split, followed by .ToList(), like so:
string csv = "one,two,three";
List<string> Strings = csv.Split(',').ToList();
I'm able to add List<string> in List<List<string>> array in this way:
List<string> first = new List<string> { "one", "two", "three" };
List<string> second = new List<string> { "four", "five", "six" };
List<List<string>> list_array = new List<List<string>> { first, second };
Now I need to create several lists populated with database records and then to add this lists to List<List<string>> array:
List<List<string>> array_list;
while (dr.Read())
{
string one = dr["Row1"].ToString();
string two = dr["Row2"].ToString();
List<string> temp_list = new List<string> { one, two };
//Here I need to add temp_list to array_list
}
Create an empty array_list:
List<List<string>> array_list = new List<List<string>>();
Then use Add method to add items:
array_list.Add(temp_list);
Change your variable declaration to initialize an empty List:
List<List<string>> array_list = new List<List<string>>();
Then, just call .Add();
List<string> temp_list = new List<string> { one, two };
//Here I need to add temp_list to array_list
array_list.Add(temp_list);
Unless i'm reading this wrong, you should just be able to do:
array_list.add(temp_list);
This should work:
array_list.Add(temp_list);
List<List<string>> array_list = new List<List<string>>();
while (dr.Read())
{
string one = dr["Row1"].ToString();
string two = dr["Row2"].ToString();
List<string> temp_list = new List<string> { one, two };
array_list.add(temp_list)
}
List<List<string>> array_list = new List<List<string>>();
while (dr.Read())
{
string one = dr["Row1"].ToString();
string two = dr["Row2"].ToString();
List<string> temp_list = new List<string> { one, two };
array_list.Add(temp_list);
}
you can add directly;
array_list.Add(temp_list);
You have to alweys remeber to make new temp_list, don't use temp_list.clear(), like I did in my project =_=.
Blockquote
List<List<string>> array_list = new List<List<string>>();
while (dr.Read())
{
string one = dr["Row1"].ToString();
string two = dr["Row2"].ToString();
List<string> temp_list = new List<string> { one, two };
array_list.Add(temp_list);
}
Blockquote