Linq To Sql需要动态在关系表中的条款帮助? [英] Linq To Sql Need Dynamic Where Clause over relational tables Help?

查看:132
本文介绍了Linq To Sql需要动态在关系表中的条款帮助?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



用户从页面选择条件,我需要帮助在LinqToSql的关系表(一对多)之间的动态where子句 。 (有4个用户选择条款的输入)



例如,来自Customer表的CompanyName和CompanyTitle以及OrderDate和ShipCity From Order表。 em>



但是,用户可以从页面界面中选择一个,动态查询将在codebehind生成并选择从LinqToSql。



您可以从另一个网页提供类似类型的示例。



alt text http://www.yazgelistir.com/Makaleler/Resimler/1000001875_Linq-Class-Server.jpg

解决方案

您是否正在寻找这样的东西,您可以在其中定义基础查询,然后评估参数以确定是否where子句是适当的?

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

如果(某些条件)
{
result = result.AsQueryable()。其中​​(x => x.companyName == name);
}
如果(某些其他条件)
{
result = result.AsQueryable()。其中​​(x => x.companyTitle == title);
}

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

我注意到您提到的一则您的评论表中没有加入外键?我不知道你如何获得一对多的关系,没有某种参照完整性或关系?


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

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

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

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.

alt text http://www.yazgelistir.com/Makaleler/Resimler/1000001875_Linq-Class-Server.jpg

解决方案

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需要动态在关系表中的条款帮助?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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