LINQ查询中的C#计算字段

时间:2018-08-17 17:22:42

标签: c# linq

我需要一些帮助来计算Linq查询中的属性。

我知道我需要在某个地方使用“ let”,但我无法弄清楚!

所以,首先,我有这种方法可以从数据库中获取列表:

public BindingList<Builders> GetListBuilders()
    {
        BindingList<Builders> builderList = new BindingList<Builders>();
        var ctx = new IWMJEntities();
        var query = (from l in ctx.tblBuilders
                     select new Builders
                     {
                         ID = l.BuilderID,
                         Projeto = l.NomeProjeto,
                         Status = l.Status,
                         DataPedido = l.DataPedido,
                         DataPendente = l.DataPendente,
                         DataEntregue = l.DataEntregue,
                         DataAnulado = l.DataAnulado
                     });
        foreach (var list in query)
            builderList.Add(list);
        return builderList;
    }

然后,我有一个函数根据状态来计算日期之间的天数:

public int GetDays()
    {
        int Dias = 0;
        foreach (var record in GetListBuilders)
        {
            if (record.Status == "Recebido")
            {
                Dias = GetBusinessDays(record.DataPedido, DateTime.Now);
            }
            else if (record.Status == "Pendente")
            {
                Dias = GetBusinessDays(record.DataPedido, (DateTime)record.DataPendente);
            }
            else if (record.Status == "Entregue")
            {
                Dias = GetBusinessDays(record.DataPedido, (DateTime)record.DataEntregue);
            }
            else if (record.Status == "Anulado")
            {
                Dias = GetBusinessDays(record.DataPedido, (DateTime)record.DataAnulado);
            }
        }
        return Dias;
    }

我需要在DataGridView中调用GetDays来提供每条记录的日期。

我的大问题是,我怎么得到这个?包括在Linq查询中?调用GetDays()(需要将每个记录的ID传递给GetDays()函数)!?

有帮助吗?

谢谢

2 个答案:

答案 0 :(得分:0)

我认为创建扩展方法会更容易:

public static int GetBusinessDays(this Builders builder) // or type of ctx.tblBuilders if not the same
{
  if (builder == null) return 0;

  switch(builder.status)
  {
    case "Recebido": return GetBusinessDays(builder.DataPedido, DateTime.Now);
    case "Pendente": return GetBusinessDays(builder.DataPedido, (DateTime)builder.DataPendente);
    case "Entregue": return GetBusinessDays(builder.DataPedido, (DateTime)builder.DataEntregue);
    case "Anulado": GetBusinessDays(builder.DataPedido, (DateTime)builder.DataAnulado);
    default: return 0;
  }
}

然后,这样称呼它:

public BindingList<Builders> GetListBuilders()
    {
        BindingList<Builders> builderList = new BindingList<Builders>();
        var ctx = new IWMJEntities();
        var query = (from l in ctx.tblBuilders
                     select new Builders
                     {
                         ID = l.BuilderID,
                         Projeto = l.NomeProjeto,
                         Status = l.Status,
                         DataPedido = l.DataPedido,
                         DataPendente = l.DataPendente,
                         DataEntregue = l.DataEntregue,
                         DataAnulado = l.DataAnulado,
                         Dias = l.GetBusinessDays()
                     });
        foreach (var list in query)
            builderList.Add(list);
        return builderList;
    }

要做得更好,要将一个对象转换为一个新对象,您应该创建一个映射器。

答案 1 :(得分:0)

为什么它需要成为查询的一部分?您无法在数据库上执行C#代码。如果您希望在数据库中完成计算,则可以创建一个视图。

只要在foreach循环中枚举了IQueryable,就会执行查询。在将每个项目添加到列表时,为什么不对它们进行枚举并设置属性呢?