LINQ methods can be used to extract a single element from an List?

This is a datatable in C# ASPNET

    DataTable dt = new DataTable();
    dt.Columns.Add("p_int", typeof(string));
    dt.Columns.Add("p_uni", typeof(string));

    dt.Rows.Add(new object[] { "0", "seat" });
    dt.Rows.Add(new object[] { "1", "X400" });
    dt.Rows.Add(new object[] { "4", "X400" });
    dt.Rows.Add(new object[] { "2", "X400" });
    dt.Rows.Add(new object[] { "2", "X4SVR" });
    dt.Rows.Add(new object[] { "3", "X400" });

With the values of datatable I have compiled two different List<string>

    List<string> StringA = new List<string> { string.Join(", ", dt.AsEnumerable().Select(x => x.Field<string>("p_int")).Distinct()) };
    List<string> StringB = new List<string> { string.Join(", ", dt.AsEnumerable().Select(z => z.Field<string>("p_uni")).Distinct()) };

I need get the value X4SVR extract from List<string> StringB and tried

    var lb = StringB.FirstOrDefault(x => x.Contains("X4SVR"));
    Response.Write(lb.ToString() + "<br /><br />");

    var result = StringB.Where(x => x.Contains("X4SVR")).FirstOrDefault();
    Response.Write(result.ToString() + "<br /><br />");

    var content = StringB.FirstOrDefault(x => x.Contains("X4SVR")) ?? StringB.FirstOrDefault();
    Response.Write(content.ToString() + "<br /><br />");

    string ls_SearchVal = "X4SVR";
    var lobj_Result = StringB.All(o => o.Contains(ls_SearchVal));
    Response.Write(lobj_Result.ToString() + "<br /><br />");

    var value = StringB.Find(item => item.Contains("X4SVR")).ToString();
    Response.Write(value.ToString() + "<br /><br />");

the return’s never only X4SVR value but are

seat, X400, X4SVR

seat, X400, X4SVR

seat, X400, X4SVR

True

seat, X400, X4SVR

How to do have this return with only value X4SVR?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

It is not understandable why do you return the value you are querying already, but:

var value = dt.AsEnumerable().Select(z => z.Field<string>("p_uni")).FirstOrDefault(s => s.Contains("X4SVR"));

Method 2

That’s because both of your lists contain only one element (of joined strings).

Do not use string.Join() for this purpose. Instead, you want something like:

var StringA = dt.AsEnumerable().Select(x => x.Field<string>("p_int")).Distinct().ToList();
var StringB = dt.AsEnumerable().Select(z => z.Field<string>("p_uni")).Distinct().ToList();

Then, you can get your value using the following:

var lb = StringB.FirstOrDefault(x => x.Contains("someString"));


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x