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();
Related
i have to list and in to list i have list of string :
public async Task<OperationResult<string>> SetAccess(AccessLevelDto accessLevels)
{
var access = await GetAccessLevels(accessLevels.RoleId);
}
private async Task<IEnumerable<string>> GetAccessLevels(Guid roleId)
{
return await AccessLevels.Where(x => x.RoleId == roleId).Select(x => x.Access).ToListAsync();
}
one list : GetAccessLevels(accessLevels.RoleId) and second accessLevels.Access
i want to find with items exist in list A and not exist in list B then put then in the var mustremove .
how can i do this ????
Use "Not Contains" LINQ expression:
var listA = new List<string> { "One", "Two", "Three" };
var listB = new List<string> { "One", "Three" };
var mustremove = listA.Where(x => !listB.Contains(x));
mustremove will contain one element: "Two"
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 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));
I am working on a hypothetical question. One of them being that if there are duplicate string collections in memory, how would I get about removing the duplicates while maintaining the original order or the collections?
try something like this
List<String> stringlistone = new List<string>() { "Hello", "Hi" };
List<String> stringlisttwo = new List<string>() { "Hi", "Bye" };
IEnumerable<String> distinctList = stringlistone.Concat(stringlisttwo).Distinct(StringComparer.OrdinalIgnoreCase);
List<List<String>> listofstringlist = new List<List<String>>() { stringlistone, stringlisttwo };
IEnumerable<String> distinctlistofstringlist = listofstringlist.SelectMany(x => x).Distinct(StringComparer.OrdinalIgnoreCase);
its depends on how you join the lists but it should give you a idea, added the ordinal ignore case in case you wanted the destinct list to treat "hi" and "Hi" as the same
you can also just call the distinct so if you did
List<String> stringlistone = new List<string>() { "Hi", "Hello", "Hi" };
stringlistone = stringlistone.Distinct(StringComparer.OrdinalIgnoreCase);
stringlistone would be a list with stringlistone[0] == "Hi" and stringlistone[1] == "Hello"
Don't worry about it. Framework does not create duplicate string in memory. All pointers with same string value points to same location in memory.
Say you have a List<List<string>> that you read from a file or database (so they're not already interned) and you want no duplicate strings, you can use this code:
public void FoldStrings(List<List<string>> stringCollections)
{
var interned = new Dictionary<string,string> ();
foreach (var stringCollection in stringCollections)
{
for (int i = 0; i < stringCollection.Count; i++)
{
string str = stringCollection[i];
string s;
if (interned.TryGetValue (str, out s))
{
// We already have an instance of this string.
stringCollection[i] = s;
}
else
{
// First time we've seen this string... add to hashtable.
interned[str]=str;
}
}
}
}