C# 使用LINQ语句在列表中使用Select和Where
这是MySQL表 在使用Visual Studio 2019、C和.NET Framework 4.7开发的ASP.NET网站主页中,我使用MySQL表的值创建了两个列表,如下所示:C# 使用LINQ语句在列表中使用Select和Where,c#,asp.net,linq,C#,Asp.net,Linq,这是MySQL表 在使用Visual Studio 2019、C和.NET Framework 4.7开发的ASP.NET网站主页中,我使用MySQL表的值创建了两个列表,如下所示: List<string> Listp_uni = new List<string>(); List<string> Listp_int = new List<string>(); Container.p_int = reader["p_int"]
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());
我需要使用LINQ从列表列表p_uni中选择等于X4SVR的值
我尝试过,但没有成功
var studentNames = Mp.Container.p_uni.Where(s => s.Mp.Container.p_uni == "X4SVR")
.Select(s => s);
错误是
编译器错误消息:CS1061:“char”不包含定义
对于'Mp',没有接受第一个参数的扩展方法'Mp'
如果缺少using指令或
汇编参考
我怎么做
编辑问题
新编辑
尝试从数据表中执行以下操作:
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());
首先,您必须了解我们不能在字符串中使用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);
看起来Mp.Container.p_uni包含一个字符串,而不是您可能期望的字符串列表。很可能是'seat'、'X400'、'X4SVR',…Where中的s参数是字符,而不是您预期的字符串。@gkulshrestha是的Mp.Container.p_uni是'seat'、'X400'、'X4SVR'@gkulshrestha出于好奇,Mp.Container.p_uni是什么?我在这段代码中找不到它:|@AmalPS好的,我用Mp.Container.p_uni编辑了这个问题。一个名为“e”的局部变量不能在这个范围内声明,因为它会赋予“e”不同的含义,它已经在“父或当前”范围内用于表示某些东西else@IterLsicIealf,如果您在此范围内已经有一个变量E,请将其全部说出来。已在“父级或当前”范围中使用。您只需通过本部分中的任何其他内容重命名e即可选择e=>e.@XDT转换好的,请在第一个问题中使用新编辑。我在问题中指出了同样的错误。。。
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);
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());
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);