C# - If list contains list - c#

I need a condition which is checking for list of string contains all list item of string array.
Example
List<string> list1 = new List<string> { "hello", "it", "is", "an", "example"};
string[] array = { "xa","lo","el","t" };
So, as you can see each items of array exist in list items. It does not matter which item of list contains the which item of array. It should just checking for each item of array is contained by any list's item. In this case I should be true.

array.All(arrayItem => list1.Any(listItem => listItem.Contains(arrayItem)));

Related

insert All Listbox items into a List

Are there a way to export the items from an Listbox into a List in c#?
i have Listbox1 that Contain some string items and I have This list:
public List<String> MyList = new List<String>();
how to add this items to Mylist?
You could do a casting like this:
public List<String> MyList = listBox1.Items.Cast<string>().ToList();
This way, to change the ListBox ObjectCollection to list of string.
You can use AddRange method:
If you are sure that all elements in Listbox1 are strings:
MyList.AddRange(ListBox1.Items.Cast<string>());
if not:
MyList.AddRange(ListBox1.Items.OfType<string>());

C# ListView to List<string>

I have a ListView with string items, and i am trying to get these items and store them in List container.
I need the reverse operation of this:
List<string> myList = new List<string> { "Item1", "item2", "item3"};
resultsList.ItemsSource = myList;
So my Question is How to Parse ListView with Items to List?
You need to cast it to IEnumerable<string>
List<string> myList = ((IEnumerable<string>)resultsList.ItemsSource).ToList();
I think you can cast and use ToList to create a list:
myList = resultsList.ItemsSource.Cast<string>().ToList();

Getting all values from Checkboxlist

I have a array of 13 strings in which I pull the string values from checkboxlist. I can pull the values that are selected with the code below. However I want to also pull the unchecked values as null. So if I select 12 values then 1 would be null in the array.
I'm not sure if the array is dynamically adding the selected values or if the code fills the uncheck values with null. Please help.Thank you.
string[] selectedAreaValues = new string[13];
IEnumerable<string> allChecked = (from item in ceCheckBoxList.Items.Cast<ListItem>()
where item.Selected
select item.Value);
selectedAreaValues = allChecked.ToArray();
If you want to always return the same number of items as in the original Items collection, but project selected items to their value and unselected items to null, then something like this should work:
IEnumerable<string> allChecked = (from item in ceCheckBoxList.Items.Cast<ListItem>()
select item.Selected ? item.Value : (string)null);
We may also use the alternative syntaxe + A concat between the two sequences
IEnumerable selectedItems = ceCheckBoxList.Items.Cast().Where(item => item.Selected).Select(item => new String(item.Value.toCharArray()));
Using a negation with the item.Selected boolean expression in the Where clause and using a Concat between the two sequences.

How to avoid duplicates when adding items to a list box in C#

I have a list boxes List1 and List2 and I want to add selected item from List2 to List1. I do as follows.
List1.Items.Add(List2.SelectedItem);
How to avoid duplicating in List1 if the same item from List2 is being added?
if (List2.SelectedItem != null
if (! List1.Items.Contains(List2.SelectedItem))
{ List1.Items.Add(List2.SelectedItem);
List2.Remove(List2.SelectedItem);
}

Populating listview with List<List<string>>

I'm coding in C# andI have a listview control with few columns, and I'm wondering, how can I add items to it from List<List<string>>. I am able to add items with
var item3 = new ListViewItem(new[] { table[0][0], table[0][1], table[0][2], table[0][3], table[0][5], table[0][7], table[0][8] });
but that doesn't seem right to me, and neither it would work because the amount of Lists is random.
You can use List<T>.ToArray to convert the list into an array which you then pass to the constructor of ListViewItem. As you are only interested in the first subitem of your table, you can just do it like this:
var item3 = new ListViewItem(table[0].ToArray());
You can use the following code to fill all the List<List<string>> into your ListView:
listView1.Items.AddRange(table.Select(list=>new ListViewItem(list.ToArray())))
.ToArray();
probably you mean like this ?
sample data
item1
List<string> list1 = new List<string>();
list1.Add("item-1.1");
list1.Add("item-1.2");
item2
List<string> list2 = new List<string>();
list2.Add("item-2.1");
list2.Add("item-2.2");
allitem
List<List<string>> listAll = new List<List<string>>();
listAll.Add(list1);
listAll.Add(list2);
string value
string sResult = string.Join(", ", from list in listAll from item in list select item);
list value
List<string> lResult = new List<string>(from list in listAll from item in list select item);
binding
lv.DataSource = lResult;
lv.DataBind();
result
item-1.1 item-1.2 item-2.1 item-2.2

Categories

Resources