微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用LINQ语句使用Select和列表中的位置

如何解决使用LINQ语句使用Select和列表中的位置

这是function action_ninja_forms_display_css( $form_id ) { if( $form_id == 22 ) { echo '<style> .ninja-forms-form-wrap{background:red} </style>'; } }; add_action( 'ninja_forms_display_css','action_ninja_forms_display_css' ) 表。

MysqL

在网站+-------+-------+ | p_int | p_uni | +-------+-------+ | 0 | seat | | 1 | X400 | | 4 | X400 | | 2 | X400 | | 2 | X4SVR | | 3 | X400 | +-------+-------+ 6 rows in set 中,用MasterPageASP.NETVisual Studio 2019开发了一个C#,我使用值创建了两个.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<string>

我需要使用'seat','X400','X4SVR' 0 1 4 2 3 List<string> Listp_uni中选择等于X4SVR的值

我尝试没有成功

LINQ

错误

编译器错误消息:CS1061:'char'不包含定义 对于“ Mp”,并且没有扩展方法“ Mp”接受第一个参数 可以找到'char'类型(您是否缺少using指令或 程序集参考?)

我该怎么办?

编辑问题

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

新编辑

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

解决方法

首先,您必须了解我们不能在字符串中使用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,"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,"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());

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。