LINQ to Entities新手问题

发布于 2024-10-31 06:32:07 字数 1085 浏览 0 评论 0原文

我不是一位经验丰富的 C# 程序员,目前我正在使用 EF 3.5 和 LINQ 做一些事情。

我有以下方法,并且我确信它可以用更好/更短的方式编写。

感谢您的帮助!

  public List<CustOrder> GetOrders(string supplierId, string locationId)
    {
        using (var ctx = new OrderEntities())
        {
            if (!string.IsNullOrEmpty(locationId))
            {
                var result = (from order in ctx.CustOrder
                              where order.SupplierId == supplierId
                                 && order.LocationId == locationId
                              select order).ToList();
                return result;
            }
            else
            {
                var result = (from order in ctx.CustOrder
                              where order.SupplierId == supplierId
                                 && order.LocationId != ""
                              select order).ToList();
                return result;              
            }
        }
    }

我的错误: 在第二个 linq 查询中,应删除以下行:

&& order.LocationId != ""

I'm not an experienced C# programmer and currently I'm doing some stuff with EF 3.5 and LINQ.

I have the following method and I'm quite sure it can be written in a better / shorter way.

Thanks for the help!

  public List<CustOrder> GetOrders(string supplierId, string locationId)
    {
        using (var ctx = new OrderEntities())
        {
            if (!string.IsNullOrEmpty(locationId))
            {
                var result = (from order in ctx.CustOrder
                              where order.SupplierId == supplierId
                                 && order.LocationId == locationId
                              select order).ToList();
                return result;
            }
            else
            {
                var result = (from order in ctx.CustOrder
                              where order.SupplierId == supplierId
                                 && order.LocationId != ""
                              select order).ToList();
                return result;              
            }
        }
    }

My mistake:
In 2nd linq query, the following line should be removed:

&& order.LocationId != ""

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

草莓味的萝莉 2024-11-07 06:32:07

我会推荐这个版本,因为我发现它更具可读性。

public List<CustOrder> GetOrders(string supplierId, string locationId)
{
    using (var ctx = new OrderEntities())
    {
        var query = from order in ctx.CustOrder
                    where order.SupplierId == supplierId
                    select order;

        if (!string.IsNullOrEmpty(locationId))
        {
            query = query.Where(o => o.LocationId == locationId)
        }

        return query.ToList();
    }
}

I would recommend this version as I find it more readable IMHO.

public List<CustOrder> GetOrders(string supplierId, string locationId)
{
    using (var ctx = new OrderEntities())
    {
        var query = from order in ctx.CustOrder
                    where order.SupplierId == supplierId
                    select order;

        if (!string.IsNullOrEmpty(locationId))
        {
            query = query.Where(o => o.LocationId == locationId)
        }

        return query.ToList();
    }
}
世界和平 2024-11-07 06:32:07

你可以做

 public List<CustOrder> GetOrders(string supplierId, string locationId)
 {
        using (var ctx = new OrderEntities())
        {
             var result = (from order in ctx.CustOrder
                          where order.SupplierId == supplierId
                          && string.IsNullOrEmpty(locationId) ? true : order.LocationId == locationId
                                  select order).ToList();
             return result;
       }
 }

you can do

 public List<CustOrder> GetOrders(string supplierId, string locationId)
 {
        using (var ctx = new OrderEntities())
        {
             var result = (from order in ctx.CustOrder
                          where order.SupplierId == supplierId
                          && string.IsNullOrEmpty(locationId) ? true : order.LocationId == locationId
                                  select order).ToList();
             return result;
       }
 }
南冥有猫 2024-11-07 06:32:07

Bala 进行空值检查,Talljoe 进行空字符串检查。但这两者兼而有之:

 public List<CustOrder> GetOrders(string supplierId, string locationId)
 {
        using (var ctx = new OrderEntities())
        {
             var result = (from order in ctx.CustOrder
                                  where order.SupplierId == supplierId
                                     && ((String.IsNullOrEmpty(locationId) && order.LocationId == locationId) || 
                                         order.LocationId.Length > 0)
                                  select order).ToList();
             return result;
       }
 }

此外,检查字符串长度通常比针对空字符串进行相等性检查更好。

Bala's does the null check and Talljoe's does the empty string check. But this one does both:

 public List<CustOrder> GetOrders(string supplierId, string locationId)
 {
        using (var ctx = new OrderEntities())
        {
             var result = (from order in ctx.CustOrder
                                  where order.SupplierId == supplierId
                                     && ((String.IsNullOrEmpty(locationId) && order.LocationId == locationId) || 
                                         order.LocationId.Length > 0)
                                  select order).ToList();
             return result;
       }
 }

Also, checking string length is usually better than equality check against an empty string.

鱼忆七猫命九 2024-11-07 06:32:07
var result = (from order in ctx.CustOrder
                          where order.SupplierId == supplierId
                             && (String.IsNullOrEmpty(locationId)
                               || order.LocationId == locationId)
                          select order).ToList();
            return result;
var result = (from order in ctx.CustOrder
                          where order.SupplierId == supplierId
                             && (String.IsNullOrEmpty(locationId)
                               || order.LocationId == locationId)
                          select order).ToList();
            return result;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文