连接两个表时的动态LINQ查询

时间:2018-08-16 04:06:44

标签: c# linq

我想为LINQ查询动态构建where子句,但我对如何执行此操作感到困惑。

这是无法编译的伪代码。给出错误消息

错误CS1503参数2:无法从'System.Linq.Expressions.BinaryExpression'转换为'System.Linq.Expressions.Expression>'

var tbl1 = Expression.Parameter(typeof(DBT_Master), "x");
var tbl2 = Expression.Parameter(typeof(DBT_MASTER_ADDRESS), "p");
var exp = Expression.And(Expression.Constant(true), Expression.Constant(true));

if (customerCode != "")
{
    exp = Expression.And(
        Expression.Constant(true), 
        Expression.Lambda<Func<DBT_Master, bool>>(
            Expression.Equal(
                Expression.Property(tbl1, "CustomerCode"),
                Expression.Constant(customerCode)
            ),
            tbl1
        )
    );
}
if (siteNo != "")
{
    exp = Expression.And(exp, 
            Expression.Lambda<Func<DBT_MASTER_ADDRESS, bool>>(
                Expression.Equal(
                    Expression.Property(tbl2, "AddressCode"),
                    Expression.Constant(siteNo)
                    ),
                tbl2
            )
        );
}

var results = (from x in db.DBT_Master
                join p in db.DBT_MASTER_ADDRESS on x.CustomerCode equals p.CustCode
                select x).Where(exp);

1 个答案:

答案 0 :(得分:1)

好吧,我找到了解决您问题的方法。我使用IQueryable模拟了它,并且效果很好。您只需要将db.DBT_Master替换为list.AsQueryable(),并将db.DBT_MASTER_ADDRESS替换为list2。

工作示例在这里:

class MainClass
    {
        public static void Main(string[] args)
        {
            List<DBT_Master> list = new List<DBT_Master>();
            list.Add(new DBT_Master { CustomerCode = "test" });
            list.Add(new DBT_Master { CustomerCode= "test1" });
            list.Add(new DBT_Master { CustomerCode = "test12" });
            list.Add(new DBT_Master { CustomerCode = "test13" });

            List<DBT_MASTER_ADDRESS> list2 = new List<DBT_MASTER_ADDRESS>();
            list2.Add(new DBT_MASTER_ADDRESS { AddressCode = "1234", CustCode="test" });
            list2.Add(new DBT_MASTER_ADDRESS { AddressCode = "asdfs", CustCode = "test" });
            list2.Add(new DBT_MASTER_ADDRESS { AddressCode = "afsadfas" });
            list2.Add(new DBT_MASTER_ADDRESS { AddressCode = "fdsa" });

            var customerCode = "test";
            var siteNo = "1234";

            var query = (from x in list.AsQueryable()
                         join p in list2.AsQueryable() on x.CustomerCode equals p.CustCode
                         select new { DBT_Master = x, DBT_MASTER_ADDRESS = p });

            if (customerCode != "")
            {
                query = query.Where(x => x.DBT_Master.CustomerCode == customerCode);
            }

            if (siteNo != "")
            {
                query = query.Where(x => x.DBT_MASTER_ADDRESS.AddressCode == siteNo);
            }

        query.Select(x => x.DBT_Master).ToList().ForEach(x => Console.WriteLine(x));
        }
    }

    class DBT_Master
    {
        public string CustomerCode { get; set; }

        public override string ToString()
        {
            return CustomerCode;
        }
    }

    class DBT_MASTER_ADDRESS {
        public string AddressCode { get; set; }
        public string CustCode { get; set; }

        public override string ToString()
        {
            return AddressCode + " " + CustCode;
        }
    }

运行该程序时,它会打印“ test”作为结果,因为只有一个数据符合给定条件(customerCode = test和AddressCode = 1234并加入CustomerCode = CustCode)