0

您好,我有一些以编程方式生成 where 语句的方法,我如何将 where 生成转移到其他任何人都可以提供帮助的类方法?

        public static List<MME.Objects.TypedLists.InvoiceList> GetList(List<MMPFramework.SearchParameter> parameter)
    {
        MME.Objects.Invoice Invoice = null;
        MME.Objects.Contractor Contractor = null;
        MME.Objects.Contract Contract = null;
        MME.Objects.TypedLists.InvoiceList invoiceList= null; 
        var t = MME.DAL.NhSessionHelper.GetCurrentSession().QueryOver<MME.Objects.Invoice>(() => Invoice);

        foreach (var searchParameter in parameter)
        {
            if(searchParameter.Expression == "Like")
            {
                t.Where(Restrictions.Like(searchParameter.PropertyName, searchParameter.ObjectValueLo));
            }
            else if (searchParameter.Expression == "Eq")
            {
                t.Where(Restrictions.Eq(searchParameter.PropertyName, searchParameter.ObjectValueLo));
            }
            else if (searchParameter.Expression == "Between")
            {
                t.Where(Restrictions.Between(searchParameter.PropertyName, searchParameter.ObjectValueLo,searchParameter.ObjectValueHi));
            }
            else if(searchParameter.Expression == "Gt")
            {
                t.Where(Restrictions.Gt(searchParameter.PropertyName, searchParameter.ObjectValueLo));
            }
            else if (searchParameter.Expression == "Lt")
            {
                t.Where(Restrictions.Lt(searchParameter.PropertyName, searchParameter.ObjectValueLo));
            }
            else
            {
                //todo more
            }
            //t.Where(Restrictions.Eq(searchParameter.PropertyName, searchParameter.ObjectValue));

        }
        t.JoinQueryOver(() => Invoice.Contractor, () => Contractor, JoinType.LeftOuterJoin)

            .JoinQueryOver(() => Invoice.Contract, () => Contract, JoinType.LeftOuterJoin)
            .Select(Projections.Property(() => Invoice.Id).WithAlias(() => invoiceList.Id),
                    Projections.Property(() => Invoice.Number).WithAlias(() => invoiceList.InvoiceNumber),
                    Projections.Property(() => Contractor.Name).WithAlias(() => invoiceList.ContractorName),
                    Projections.Property(() => Contract.Number).WithAlias(() => invoiceList.ContractNumber)
            )
            .TransformUsing(Transformers.AliasToBean<MME.Objects.TypedLists.InvoiceList>());
        return t.List<MME.Objects.TypedLists.InvoiceList>().ToList();

    }

我已经尝试过,但它似乎不起作用....希望有人在做某事并可以帮助我处理它。

    public class BaseList
{
    public object WhereGenerator(object ob)
    {
        QueryOver Ded = ob as QueryOver;
        return null;
    }
}
4

1 回答 1

1
foreach (var restriction in BaseList.Createrestrictions(parameter))
{
    t.Where(restriction);
}


public class BaseList
{
    public IEnumerable<AbstractCriterion> Createrestrictions(List<MMPFramework.SearchParameter> parameter)
    {
        return parameter.Select(ToCritieria);
    }

    private AbstractCriterion ToCritieria(SearchParameter searchParameter)
    {
        if(searchParameter.Expression == "Like")
        {
            return Restrictions.Like(searchParameter.PropertyName, searchParameter.ObjectValueLo);
        }
        else ...
    }
}
于 2012-05-15T10:02:29.370 回答