如何将此SQL转换为LINQ?
我现在正在阅读它,但只是把它放在那里以防万一我无法做到。
SqlConnection connection = new SqlConnection
{
ConnectionString = ConfigurationManager.ConnectionStrings["HBOS"].ConnectionString
};
connection.Open();
foreach (ExchangeRateData x in exchangeRateDatas.ExchangeRateDataList)
{
SqlCommand cmd = new SqlCommand("UPDATE dbo.CurrencyExchange " +
"SET Rate = '" + x.Rate + "', DateTimeStamp = CAST('" + x.TimeStamp +
"' AS DATETIME), CreatedBy = '" + x.CreatedBy + "', RateInv = '" +
x.RateInv + "' " +
"WHERE Currency = '" + x.ToCurrency + "';", connection);
// Sql query and connection
cmd.ExecuteNonQuery();
}
connection.Close();
答案 0 :(得分:3)
首先创建dbcontext
然后
CurrencyExchange CurrencyExchangeObject = context.CurrencyExchange
.Where(a => a.Currency = x.ToCurrency)
.FirstOrDefault();
之后,您可以简单地分配值 喜欢
CurrencyExchangeObject.Rate = x.Rate;
CurrencyExchangeObject.DateTimeStamp = Convert.ToDateTime(x.TimeStamp);
然后简单地说
context.SaveChanges();
答案 1 :(得分:0)
听起来你的老板正在寻找LINQ to SQL实现。遗憾的是,您的问题没有快速回答,因为添加此功能需要的不仅仅是将查询转换为LINQ",因为需要设置一些环境来支持它
您可能想要从该主题的一些基本Google搜索开始:
第一对结果:
http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx http://msdn.microsoft.com/en-us/library/bb386976(v=vs.110).aspx
LINQ to SQL有一个更广泛使用的表兄,称为Entity Framework,它不依赖于SQL Server。您可能也想考虑这一点。