Linq to sql between List and Comma separated string - c#

I Have one list and one comma separated column value.
lst.Add("Beauty");
lst.Add("Services");
lst.Add("Others");
And Column value is like
Beauty,Services
Service,Others
Beauty,Others
Other,Services
Other
Beauty, Food
Food
Now I Want all those rows which contains any list item.
Output result is
Beauty,Services
Service,Others
Beauty,Others
Other,Services
Other
Beauty, Food
First Edit
These comma separated values are one of my table's one column value.
I need to check against that column.

Assuming you can figure out how to get the RowCollection
RowCollection.Where(row => row.Split(",").ToList().Where(x => list.Contains(x)).Any()).ToList();
will evaluate to true if the row contains a value from the list.

try
List<string> lst = ....; // your list of strings
string[] somecolumnvalues = new string[] { "", "",... }; // your columvalues
var Result = from l in lst from c in somecolumnsvalues where c.Contains (l) select c;

Related

How to get columns with values from second list which is contain in first list?

I have two list. First list has matched column which is available in second list.
I want to show matched columns with values from second list.
List<string> _filtredData = new List<string>();
_filtredData.Add("Broker");
_filtredData.Add("Loaction");
_filtredData.Add("StandardLineItem");
_filtredData.Add("Section");
foreach (DataColumn _dtCol in FinalDiffData.Columns)
{
if (matchedItems.Contains(_dtCol.ToString()))
{
_filtredData.Add(_dtCol.ToString());
}
}
_filtredData -> contains matched columns which available in Second list.
FinalDiffData.AsEnumerable() -> this is secondlist.
List<string> _filtredData = new List<string>();
_filtredData.Add("Broker");
_filtredData.Add("Loaction");
_filtredData.Add("StandardLineItem");
_filtredData.Add("Section");
foreach (DataColumn _dtCol in FinalDiffData.Columns)
{
if (matchedItems.Contains(_dtCol.ToString()))
{
_filtredData.Add(_dtCol.ToString());
}
}
var shortedListMismatchElementLocal = _filtredData;
var result = FinalDiffData.AsEnumerable().Where(p =>
shortedListMismatchElementLocal.Any());
Please help me with proper answer.
Edit from your last comment
FinalDiffData.AsEnumerable() list has column like
Broker, Loaction, StandardLineItem, Section, 2Q2019E, 3Q2019E, 4Q2019E, 2019E, 1Q2020E
etc as earning order. _filtredData list has
Broker, Loaction, StandardLineItem, Section, 2Q2019E, 3Q2019E, 4Q2019E,
I want to get matched column with value from FinalDiffData.AsEnumerable() which available in _filtredData list
You need to compare a List<string> with a DataTable. You're only interested in one column in the datatable (I'll assume the column name is "Data").
The following query will get all rows in column "Data" matching _filteredData.
var result = FinalDiffData
.AsEnumerable()
.Where(p => _filtredData.Contains(p["Data"])); // use the actual column name here

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.

c# - get multiple listbox values and turn into comma separated string winforms

I want to get all the selected list items in my listbox and put their values into a comma separated string. I populate the listbox using a List containing my custom object TestSubject which contains a TestSubjectID and TestSubjectName:
var objTestSubjectList = new List<TestSubject>();
//code that adds items to List
I then map the Listbox datasource to this list and set the DisplayMember, ValueMember correspondingly:
lbTestSubjects.DataSource = objTestSubjectList;
lbTestSubjects.DisplayMember = "TestSubjectName";
lbTestSubjects.ValueMember = "TestSubjectID";
When the multiple items in the listbox have been selected, I want to grab the values for each item (the TestSubjectID) and place them in a comma separated string. For example, if 3 items are selected with TestSubjectIDs of 10 20 and 30 I want to loop through and grab the values and put them in a string like so: "10,20,30". I am using windows forms. How would I accomplish this?
You need to loop over the SelectedIndices collection, retrieve the TestSubject object stored in the Item corresponding to the index selected and then add its TestSubjectID value to a list of integers. Finally string.Join will create the string for you.
List<int> ids = new List<int>();
foreach(int x in lbTestSubjects.SelectedIndices)
{
TestSubject t = lbTestSubjects.Items[x] as TestSubject ;
ids.Add(t.TestSubjectID);
}
string result = string.Join(",", ids);
Console.WriteLine(result);
This code could also be reduced a lot using LINQ
var ri = lbTestSubjects.SelectedIndices
.OfType<int>()
.Select(i => ((TestSubject)lbTestSubjects.Items[i]).TestSubjectID);
string result = string.Join(",", ri);

Linq: select where in List<string>

Struggeling with some LinqToExcel filtering here...
Ive got a List<string> columnsToFilter that contains 9 strings, and with that i want to filter out the data for certain columns in a List<Row>, where Row contains the properties
IEnumerable<string> ColumnNames
Cell this[string columnName]
So: List<Row> has say 30 rows, each having 12 ColumnNames. Now i want to filter that List<Row> using List<string> columnsToFilter so that i end up with a List<Row> of 30 rows and 9 ColumnNames.
I can select the data for one column by quering the columnname:
var result = content.Select(m => m["Column1"]).ToList();
Now i want to filter the data based on a List of strings List<string> columnsToFilter. Whats the best way to achieve that?
Is this what you are looking for?
var colnames = new List<string>();
var rows = new Dictionary<string, object>();
var result = rows.Where(kv => colnames.Contains(kv.Key)).Select(kv => kv.Value);
Define an object called MyObject which has the property names corresponding to the 9 columns that you want to select.
var excel = new ExcelQueryFactory("excelFileName");
var myObjects = from c in excel.Worksheet<MyObject>()
select c;
Bingo. You can now iterate through the 30 objects with the 9 columns as properties.
Remember that LinqToExcel will happily fill objects even if they don't have all the columns represented.
You could even have a property or method as part of MyObject called "row" that would be a Dictionary() object so you could say myObject.row["ColumnName"] to reference a value if you preferred that syntax to just saying myObject.ColumnName to get the value. Personally I would rather deal with actual properties than to use the dictionary convolution.
I ended up doing this in two steps:
foreach (var column in columnNumbers)
{
yield return data.Select(m => m[column].Value.ToString()).ToList();
}
Now I have the data I need, but with the rows and columns swapped, so i had to swap rows for columns and vice versa:
for (int i = 1; i < rowCount; i++)
{
var newRow = new List<string>();
foreach (var cell in list)
{
newRow.Add(cell[i]);
}
yield return newRow;
}

DataTable's Row's First Column to String Array

I have a DataTable. I want to get every rows first column value and append to a string array. I do not want to use foreach looping for every row and adding to string array. I tried this, but stuck at some point
DataRow[] dr = new DataRow[dtCampaignSubscriberLists.Rows.Count];
dtCampaignSubscriberLists.Rows.CopyTo(dr, 0);
string[] array = Array.ConvertAll(dr, new Converter<DataRow, String>(????));
Thanks
string[] array = yourTable
.AsEnumerable()
.Select(row => row.Field<string>("ColumnName"))
.ToArray();
You could do something like:
dtCampaignSubscriberLists.AsEnumerable().Select(r => r[0].ToString()).ToArray();

Categories

Resources