使用LINQ to SQL的关系表上的动态Where子句 [英] Dynamic Where Clause over relational tables with LINQ to SQL

查看:101
本文介绍了使用LINQ to SQL的关系表上的动态Where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于LinqToSql中的关系表(一对多)上的动态where子句,我需要帮助.

I need help for a dynamic where clause over relational tables (one to many) in LinqToSql.

用户从页面选择条件. (有4个输入供用户选择条款)

User select conditions from page. (there is 4 input that user select the clauses)

例如,Customer表中的CompanyName和CompanyTitle以及Order表中的OrderDate和ShipCity.

For example CompanyName and CompanyTitle from Customer table and OrderDate and ShipCity From Order table.

但是用户可以从页面界面中选择一个或多个,动态查询将在代码隐藏处生成,然后选择From LinqToSql.

But user can select one ore many of them from page interface and dynamic query will be generated at codebehind and select From LinqToSql.

您可以在其他网页上提供类似类型的示例.

You can give similar type of example from another web pages.

推荐答案

您是否正在寻找类似的东西,在哪里定义基本"查询,然后评估参数以确定where子句是否合适?

Are you looking for something like this, where you define the "base" query, and then evaluate parameters to determine if a where clause is appropriate?

var result = (from x in context.X
              select x);

if(some condition)
{
    result = result.AsQueryable().Where(x => x.companyName == name);
}
if(some other condition)
{
    result = result.AsQueryable().Where(x => x.companyTitle == title);
}

//return result.ToList();
//return result.FirstOrDefault();
//return result.Count(); //etc

我在您的一条评论中注意到您提到您的表没有外键联接吗?我不确定在没有某种参照完整性或关联性的情况下如何获得一对多关系?

I noticed in one of your comments you mentioned your tables are not joined by a foreign key? I'm not sure how you get a one-to-many relationship without some kind of referential integrity or relationship?

这篇关于使用LINQ to SQL的关系表上的动态Where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆