【问题标题】:How to create an if else statement in C# with Linq如何使用 Linq 在 C# 中创建 if else 语句
【发布时间】: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


    【解决方案1】:

    假设您想在oneOrder.ProductID == 'A' 时屏蔽产品名称...

    results.Add(new CustomerOrderHistory
    {
        /*...*/,
        ProductName = (oneOrder.RecordID == 'A' ? "Archived Product" : oneOrder.ProductName),
        /*... */
    });
    

    使用conditional operator赋值时可以放置条件。

    或者,您可以在实例化对象之前将其存储在变量中:

    var productName = oneOrder.ProductName;
    if (oneOrder.ProductID == 'A')
    {
        productName = "Archived Product";
    }
    results.Add(new CustomerOrderHistory
    {
        /*...*/,
        ProductName = productName,
        /*... */
    });
    

    【讨论】:

    • 您的第一个选项效果很好。如果有第三种选择怎么办。如 One.RecordID == 'B' , 已删除产品
    • 在第二个例子中使用 else if 语句
    • @ChrisPasa: (oneOrder.ProductID == 'A' ? "Archived Product" : oneOrder.ProductID == 'B' ? "B Product" : oneOrder.ProductName)
    • @BradChristie 谢谢我今天早上想通了
    猜你喜欢
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多