简体   繁体   中英

how to retrieve values by LINQ in asp.net?

I want to set value to a literal control using LINQ. I got the result from database in var by the following code:

var result=md.StoredProc_Name(id);

Now I want to assign particular columns value to a literal. As we can do simply in asp.net as bellow with the help of datatable,

dt=obj.Test(id);
ltrlName.Text=dt.Rows[0]["Name"].ToString();
ltrlAddress.Text=dt.Rows[0]["Address"].ToString();

How can we do the same thing in LINQ?

var first = result.FirstOrDefault();

if (first != null)
{
   ltrlName.Text = first.Name;
   ltrlAddress.Text = first.Address;
}

Addendum - How to do this without linq to objects:

With the code below in a class called DB

var result = DB.SelectIntoItem("StoredProc_Name",
                               connectionString,
                               System.Data.CommandType.StoredProcedure,
                               new { param1 = "val1" });

if (!reader.Empty)
{
   ltrlName.Text=result.Name;
   ltrlAddress.Text=result.Address;
}
etc.

Code

public static dynamic SelectIntoItem(string SQLselect, string connectionString, CommandType cType = CommandType.Text, object parms = null)
{
  using (SqlConnection conn = new SqlConnection(connectionString))
  {
    using (SqlCommand cmd = conn.CreateCommand())
    {
      dynamic result = new System.Dynamic.ExpandoObject();

      cmd.CommandType = cType;
      cmd.CommandText = SQLselect;

      if (parms != null)
        Addparms(cmd, parms);

      conn.Open();


      using (SqlDataReader reader = cmd.ExecuteReader())
      {
        if (reader.Read())  // read the first one to get the columns collection
        {
          var cols = reader.GetSchemaTable()
                       .Rows
                       .OfType<DataRow>()
                       .Select(r => r["ColumnName"]);

          foreach (string col in cols)
          {
            ((IDictionary<System.String, System.Object>)result)[col] = reader[col];
          }
          result.Empty = false;

          if (reader.Read())
          {
            // error, what to do?
            result.Error = true;
            result.ErrorMessage = "More than one row in result set.";
          }
          else
          {
            result.Error = false;
          }

        }
        else
        {
          result.Empty = true;
          result.Error = false;
        }
      }

      conn.Close();

      return result;
    }
  }
}

private static void Addparms(SqlCommand cmd, object parms)
{
  // parameter objects take the form new { propname : "value", ... } 
  foreach (PropertyInfo prop in parms.GetType().GetProperties())
  {
    cmd.Parameters.AddWithValue("@" + prop.Name, prop.GetValue(parms, null));
  }
}

If you are insterested follow my GitHub, I'll be making the rest of it public soon ( GitHub )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM