问题描述:

How do you convert this SQL to LINQ?

I'm reading it now, but just putting this out there in case I can't do it.

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

网友答案:

Create a dbcontext first

then

CurrencyExchange CurrencyExchangeObject = context.CurrencyExchange
                                         .Where(a => a.Currency = x.ToCurrency)
                                         .FirstOrDefault();

after that you can simple assign the values like

CurrencyExchangeObject.Rate = x.Rate;
CurrencyExchangeObject.DateTimeStamp  = Convert.ToDateTime(x.TimeStamp);

and then simply say

context.SaveChanges();
网友答案:

Sounds like your boss is looking for a LINQ to SQL implementation. Unfortunately, your question does not have a quick answer because adding this functionality requires a lot more than just "converting a query to LINQ", as there are a number of things needed to get your environment set up to support it.

You may want to start with some basic Googling of the topic:

First couple results:

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 has a more widely-used cousin called Entity Framework, which is not dependent upon SQL Server. You may want to consider that as well.

相关阅读:
Top