【问题标题】:Using Select and Where in a List using LINQ statement使用 LINQ 语句在列表中使用 Select 和 Where
【发布时间】:2021-01-30 00:40:18
【问题描述】:

这是MySQL 表。

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

在网站的MasterPage 开发了一个ASP.NETVisual Studio 2019C#.NET Framework 4.7,我使用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&lt;string&gt;的回报是

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

我需要使用LINQList&lt;string&gt; Listp_uni中选择等于X4SVR的值

我试过了,没有成功

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

错误是

编译器错误消息:CS1061:'char' 不包含定义 对于 'Mp' 并且没有扩展方法 'Mp' 接受第一个参数 可以找到类型“char”(您是否缺少 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);

【问题讨论】:

  • 看起来 Mp.Container.p_uni 包含单个字符串,而不是您可能期望的字符串列表。很可能是"'seat','X400','X4SVR',..."
  • Where 中的s 参数是char,而不是您所期望的string
  • @gkulshrestha 是的,Mp.Container.p_uni'seat','X400','X4SVR'
  • @gkulshrestha 好奇什么是 Mp.Container.p_uni ?我在这段代码中找不到它:|
  • @AmalPS 好的,我已经用Mp.Container.p_uni 编辑了这个问题

标签: c# asp.net linq


【解决方案1】:

首先你必须明白我们不能在字符串中使用 select。 主要功能在链接中请通过String functions。 在下面的代码中,我创建了一些选择示例

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

【讨论】:

    【解决方案2】:

    尝试从数据表中进行以下操作:

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

    【讨论】:

    • 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,错误说明你在这个范围内已经有一个变量 E。 “已在‘父级或当前’范围内使用”。您可以在此部分中将 e 简单地重命名为 Select(e =&gt; e.
    • @xdtTransform 好的,请在第一个问题中查看新编辑。我在问题中指出了相同的错误...
    猜你喜欢
    • 1970-01-01
    • 2015-07-26
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多