使用 LINQ to SQL 对关系表的动态 Where 子句
- 作者: DGBOY
- 来源: 51数据库
- 2022-12-15
问题描述
我需要关于 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.
但是用户可以从页面中选择一个或多个界面,动态查询将在代码隐藏处生成并选择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?
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
