簡體   English   中英

使用 LINQ 語句在列表中使用 Select 和 Where

[英]Using Select and Where in a List using LINQ statement

這是MySQL表。

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

在使用Visual Studio 2019C#.NET Framework 4.7開發ASP.NET的網站的MasterPage中,我使用 MySQL 表的值創建了兩個List<string> ,如

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());

這些List<string>的返回是

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

我需要從選擇List<string> Listp_uni值等於X4SVR使用LINQ

我嘗試過但沒有成功

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

錯誤是

編譯器錯誤消息:CS1061:“char”不包含“Mp”的定義,並且找不到接受“char”類型的第一個參數的擴展方法“Mp”(您是否缺少 using 指令或程序集引用?)

我該怎么做?

編輯問題

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();
            }
        }
    }

新編輯

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);

首先,您必須了解我們不能在字符串中使用 select。 主要函數在鏈接中,請通過它字符串函數 在下面的代碼中,我創建了一些用於選擇的示例

“X4SVR” 請檢查。 我的代碼不是更好的代碼。但它會給你一些解決問題的見解。

快樂編碼:)

        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);

嘗試從數據表中執行以下操作:

            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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM