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

查看:28
本文介绍了使用 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 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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