How to search string on list
Work on C#.I have a dataset I want to search string on this dataset table.
DataSet ds = new DataSet();
ds = ImportFromExcel(oOpenFileDialog.FileName, false);
...
var w = (from p in ds.Tables["ReadExcel"].开发者_Python百科Rows.Cast<DataRow>().ToArray()
select p.ItemArray).ToList();
string[] sExcelString = {"Vessel Name :", "Voyage No. :", "Port :", "Terminal :",
"NMC Global File :" };
from this w list I want to search sExcelString string arrays all element .After search I want to get the list to retrive the related values.How to search on list and how to get the related list for next process.Bellow picture describe detail.
If have any query plz ask?Thanks in advance.
The first thing you want to do is re-write your "w" expression like this:
var w = from p in ds.Tables["ReadExcel"].AsEnumerable() select p.ItemArray;
.ToArray() and .ToList() should be saved as options of last resort — for example if you need to pass the expression to a function that requires an array or if you really want to protect the result against multiple iterations. Calling them too soon forces an iteration of your data, and prevents you from using the nice composition properties of linq to narrow your result first (and thus save big on memory and execution time).
Once that is done, you can add additional "from" and "where" directives to join the data like so:
var w = from p in ds.Tables["ReadExcel"].AsEnumerable()
where p.ItemArray.Intersect(sExcelString).Any()
select p.ItemArray;
Note that I rarely use the query comprehension syntax; I prefer the function calls/lambda notation. I also typed this right into the browser. Put the two together and it's unlikely to work "as is". The important thing here is that this should get you 90% of the way there. Consider the last 10% a chance to really understand how this all works, so that you can better write future expressions.
After put the Excel value in dataset ds.Tables["ReadExcel"]**string array **sExcelString3 values are retrieve respectively.Bellow syntax help to retrieve string values.
string[] sExcelString3 = { "Vessel Name :", "Voyage No. :", "Port :", "Terminal :", "NMC Global File :","Line : ","ETA : ","Total Container : " };
//ItemArray);
var w = (from p in ds.Tables["ReadExcel"].Rows.Cast<DataRow>().ToArray() select p.ItemArray).ToList();
foreach (string item in sExcelString2)
{
var r3 = (from c in w
where c.Any(e => e.ToString().Contains(item))
select c).ToList();
System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(item);
foreach (var it in r3)
{
// int r = it.ToList().FindIndex(p => regEx.IsMatch(p.ToString()));
string result =r3[ it.ToList().FindIndex(p => regEx.IsMatch(p.ToString()))+1];
}
}
精彩评论