【发布时间】:2016-09-15 11:30:33
【问题描述】:
我正在使用 linq to sql 创建一个 web api。我需要在我的.我在网上找不到任何东西似乎每个人都在使用实体框架。
public List<customerorderhistory> GetCustomerOrderHistory(string customerID)
{
try
{
List<customerorderhistory> results = new List<customerorderhistory>();
NorthwindDataContext dc = new NorthwindDataContext();
foreach (CustOrderHistResult oneOrder in dc.CustOrderHist(customerID))
{
results.Add(new CustomerOrderHistory()
{
ProductName = oneOrder.ProductName,
Total = oneOrder.Total ?? 0
});
}
return results;
}
catch (Exception ex)
{
// Return any exception messages back to the Response header
OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse;
response.StatusCode = System.Net.HttpStatusCode.InternalServerError;
response.StatusDescription = ex.Message.Replace("\r\n", "");
return null;
}
}
他是我尝试过的。我想我可能将 if 语句放在错误的位置。任何帮助将不胜感激。
public List<customerorderhistory> GetCustomerOrderHistory(string customerID)
{
try
{
List<customerorderhistory> results = new List<customerorderhistory>();
NorthwindDataContext dc = new NorthwindDataContext();
foreach (CustOrderHistResult oneOrder in dc.CustOrderHist(customerID))
{
results.Add(new CustomerOrderHistory()
{
if (oneOrder.RecordID == 'A')
{
ProductName = "Archived Product"
}
else
{
ProductName = oneOrder.ProductName,
}
Total = oneOrder.Total ?? 0
});
}
return results;
}
catch (Exception ex)
{
// Return any exception messages back to the Response header
OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse;
response.StatusCode = System.Net.HttpStatusCode.InternalServerError;
response.StatusDescription = ex.Message.Replace("\r\n", "");
return null;
}
}
【问题讨论】:
标签: c# linq visual-studio web-services asp.net-web-api