Dynamic Where Clause over relational tables with LINQ to SQL(使用 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.
但是用户可以从页面中选择一个或多个界面,动态查询将在代码隐藏处生成并选择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 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 LINQ to SQL 对关系表的动态 Where 子句


- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 使用 rss + c# 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01