1

This is the MySQL table.

+-------+-------+
| p_int | p_uni |
+-------+-------+
|     0 | seat  |
|     1 | X400  |
|     4 | X400  |
|     2 | X400  |
|     2 | X4SVR |
|     3 | X400  |
+-------+-------+
6 rows in set

In the MasterPage of website developed an ASP.NET with Visual Studio 2019, C# and .NET Framework 4.7, I have create two List<string> using the values of MySQL table, as

List<string> Listp_uni = new List<string>();
List<string> Listp_int = new List<string>();

Container.p_int = reader["p_int"].ToString();
Container.p_uni = reader["p_uni"].ToString();

Listp_uni.Add(Container.p_uni.ToString());
Listp_int.Add(Container.p_int.ToString());

Container.p_uni = string.Join(",", Listp_uni.Select(e => "'" + e + "'").Distinct());
Container.p_int = string.Join(" ", Listp_int.Select(e => "" + e + "").Distinct());

the return's of these List<string> are

'seat','X400','X4SVR'
0 1 4 2 3

I need select from the List<string> Listp_uni the value equal to X4SVR using LINQ

I have try without success

var studentNames = Mp.Container.p_uni.Where(s => s.Mp.Container.p_uni == "X4SVR")
                   .Select(s => s);

the error is

Compiler Error Message: CS1061: 'char' does not contain a definition for 'Mp' and no extension method 'Mp' accepting a first argument of type 'char' could be found (are you missing a using directive or an assembly reference?)

How can I do it?

Editing question

public static class Container
{
    public static string p_int
    {
        get
        {
            if (HttpContext.Current.Session["p_int"] != null)
            {
                return HttpContext.Current.Session["p_int"].ToString();
            }
            return null;
        }
        set
        {
            HttpContext.Current.Session["p_int"] = value;
        }
    }

    public static string p_uni
    {
        get
        {
            if (HttpContext.Current.Session["p_uni"] != null)
            {
                return HttpContext.Current.Session["p_uni"].ToString();
            }
            return null;
        }
        set
        {
            HttpContext.Current.Session["p_uni"] = value;
        }
    }
}


    string constr = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;

    using (MySqlConnection con =
        new MySqlConnection(constr))
    {
        using (MySqlCommand cmd =
            new MySqlCommand())
        {
            try
            {
                if (username != null)
                {
                    con.Open();
                    cmd.Connection = con;
                    cmd.CommandText = "SP_ML_AUTE";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("tusername", username.ToString().ToUpper());

                    using (MySqlDataReader reader =
                        cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                Container.p_int = reader["p_int"].ToString();
                                Container.p_uni = reader["p_uni"].ToString();
                                Listp_uni.Add(Container.p_uni.ToString());
                                Listp_int.Add(Container.p_int.ToString());
                            }

                            Container.p_uni = string.Join(",", Listp_uni.Select(e => "'" + e + "'").Distinct());
                            Container.p_int = string.Join(" ", Listp_int.Select(e => "" + e + "").Distinct());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("operation failed!", ex);
            }
            finally
            {
                cmd.Connection.Close();
            }
        }
    }

new editing

DataTable dt = new DataTable();
dt.Columns.Add("p_int", typeof(int));
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" });

string p_int = string.Join(",", dt.AsEnumerable().Select(x => x.Field<int>("p_int")).Distinct());
string p_uni = string.Join(" ", dt.AsEnumerable().Select(z => z.Field<string>("p_uni")).Distinct());

Response.Write(p_int + "<br />" + p_uni + "<br /><br />");

var studentNames = p_uni.Where(s => s.p_uni == "X4SVR").Select(s => s);

Response.Write(studentNames);
6
  • 1
    Looks like Mp.Container.p_uni contains a single string and not a list of strings as you might be expecting. Most probably it is "'seat','X400','X4SVR',..." Commented Oct 15, 2020 at 14:03
  • Your s parameter inside the Where is a char, not string as you have expected. Commented Oct 15, 2020 at 14:05
  • @gkulshrestha Yes the Mp.Container.p_uni is 'seat','X400','X4SVR' Commented Oct 15, 2020 at 14:09
  • @gkulshrestha for curiosity what is Mp.Container.p_uni ? I couldn't find it in this code :| Commented Oct 15, 2020 at 14:12
  • @AmalPS okay, I have edited the question with Mp.Container.p_uni Commented Oct 15, 2020 at 14:25

2 Answers 2

2

Firstly you have to understand we cannot use select in string. The main functions are in the link please go through it String functions . In the below code I've created some examples for selecting

"X4SVR" . Please check. My code is not a better one .But it will give you some insight to solve your issue.

Happy coding :)

        DataTable dt = new DataTable();
        dt.Columns.Add("p_int", typeof(int));
        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" });

        string p_int = string.Join(",", dt.AsEnumerable().Select(x => x.Field<int>("p_int")).Distinct());
        string p_uni = string.Join(" ", dt.AsEnumerable().Select(z => z.Field<string>("p_uni")).Distinct());

        Response.Write(p_int + "<br />" + p_uni + "<br /><br />");

        // if you want to get p_uni and p_int take from the datatable itself
        //it will return matching  p_uni and p_int
        var studentNames = dt.AsEnumerable().Where(s => s.Field<string>("p_uni") == "X4SVR").Select(y => y);

        //if you want get the name only
        //Method - using the array
        string [] p_uniArray = dt.AsEnumerable().Select(z => z.Field<string>("p_uni")).ToArray();
        var studentNames1 = p_uniArray.AsEnumerable().Where(s => s == "X4SVR").Select(y => y);

        //if you need to take from p_uni string it self please convert it into array  like
        //here you are not using any separator so I'm splitting it with space

        var stName = p_uni.Split(" ");
        var studentNames2 = stName.Where(x => x == "X4SVR").Select( y => y).Distinct();

      //  var studentNames = dt.Where(s => s.p_uni == "X4SVR").Select(s => s);

        Response.Write(studentNames);
Sign up to request clarification or add additional context in comments.

Comments

0

Try following from a datatable :

            DataTable dt = new DataTable();
            dt.Columns.Add("p_int", typeof(int));
            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"});


            string p_int = string.Join(",", dt.AsEnumerable().Select(e => e.Field<int>("p_int")).Distinct());
            string p_uni = string.Join(" ", dt.AsEnumerable().Select(e => e.Field<string>("p_uni")).Distinct());

3 Comments

A local variable named 'e' cannot be declared in this scope because it would give a different meaning to 'e', which is already used in a 'parent or current' scope to denote something else
@IterLsicIealf, error say it all you already have a variable E in this scope. "is already used in a 'parent or current' scope". You can simply rename e by anything else in this part Select(e => e.
@xdtTransform Okay, please se new editing in first question. I have the same error indicate in the question...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.