LINQ to SQL的需要动态Where子句在关系表帮助吗? [英] Linq To Sql Need Dynamic Where Clause over relational tables Help?

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

问题描述

我需要帮助的动态where子句通过关系表(一对多)在LinqToSql。

I need Help for 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)

例如公司名称和CompanyTitle从客户表和订购日期和货主国家从订单表。

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

用户可以选择一个或其中许多来自页接口和动态查询将在codebehind生成并选择从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天全站免登陆