Using LINQ and Storing Results in a DataTable

Firstly, I can say that I'm pretty new to programming and C #.

I am using LINQ to query the DataTable, which I made for specific results. I know this snippet works, but for some reason, when I run it through the VS debugger, it allocates the following variable:

var resultOne = from Rows in dt.AsEnumerable()
                where Rows.Field<string>("Column1") == SLSRResult
                //&& row.Field("B") == 2
                select Rows.Field<string>("Column2");
MessageBox.Show(string.Format ("Value is: {0}", resultOne));

I apologize if this question is unclear or if the question itself is not in the correct format, this is the first time I use this resource.

+4
source share
1 answer

resultOne- This is not one line, but IEnumerable<string>, therefore, several are possible. You either have to choose a specific one (fe The First()), or use another method to get one, for example String.Join:

string allResults = String.Join(",",  resultOne);

foreach:

foreach(string str in resultOne)
    MessageBox.Show(string.Format ("Value is: {0}", str));

, , First FirstOrDefault ( Single/SingleOrDefault, ):

string firstOfAll = resultOne.FirstOrDefault(); // null if there was none
+1

Source: https://habr.com/ru/post/1617976/


All Articles