ASP.NETMVC将Sql转换为Linq

ASP.NETMVC将Sql转换为Linq,linq,entity-framework,Linq,Entity Framework,我正在更新一个旧应用程序,使用EF和Linq。我在其中一个查询中遇到问题-在SQL中是: SELECT id, type_id, rule_name, rule_start, rule_end, rule_min FROM Rules WHERE (rule_min > 0) AND (rule_active = 1) AND (rule_fri = 1) AND ('2012-01-01' BETW

我正在更新一个旧应用程序,使用EF和Linq。我在其中一个查询中遇到问题-在SQL中是:

SELECT    id, type_id, rule_name, rule_start, rule_end, rule_min
FROM      Rules
WHERE     (rule_min > 0) 
          AND (rule_active = 1) 
          AND (rule_fri = 1) 
          AND ('2012-01-01' BETWEEN rule_start AND rule_end) 
          AND (id IN
                      (SELECT      rule_id
                        FROM       RulesApply
                        WHERE      (type_id = 3059)))
ORDER BY pri
到目前为止,我已经:

         var rules = db.Rules.Include("RulesApply")
                .Where(t => (t.rule_active == 1)
                    && (t.rule_min > 0)
                    && (dteFrom >= t.rule_start && dteFrom <= t.rule_end)
                    && (this is where I'm stuck)
                    )
                    .OrderBy(r => r.pri);
模型包括:

public class Rule
{
    [Key]
    public Int64 id { get; set; }
    public Int64 hotel_id { get; set; }
    public byte rule_active { get; set; }
    public DateTime rule_start { get; set; }
    public DateTime rule_end { get; set; }
    public int rule_min { get; set; }
    public int pri { get; set; }
    public virtual ICollection<RuleApply> RulesApply { get; set; }
}

public class RuleApply
{

    [Key, Column(Order = 0)]
    public Int64 type_id { get; set; }
    [Key, Column(Order = 1)]
    public Int64 rule_id { get; set; }
    [ForeignKey("rule_id")]
    public virtual Rule Rule { get; set; }
}
公共类规则
{
[关键]
公共Int64 id{get;set;}
公共Int64酒店{get;set;}
公共字节规则_活动{get;set;}
公共日期时间规则_start{get;set;}
公共日期时间规则_end{get;set;}
公共整数规则_min{get;set;}
公共int pri{get;set;}
公共虚拟ICollection规则使用{get;set;}
}
公共类规则适用
{
[键,列(顺序=0)]
public Int64 type_id{get;set;}
[键,列(顺序=1)]
公共Int64规则\u id{get;set;}
[外键(“规则id”)]
公共虚拟规则规则{get;set;}
}
有人能帮我完成这个查询吗

谢谢,

标记

尝试这样做:

var rules = db.Rules.Include("RulesApply")
                .Where(t => (t.rule_active == 1)
                    && (t.rule_min > 0)
                    && (dteFrom >= t.rule_start && dteFrom <= t.rule_end)
                    && t.RulesApply.Any(a => a.type_id == 3059)
                    .OrderBy(r => r.pri);
var rules=db.rules.Include(“RulesApply”)
.其中(t=>(t.rule_active==1)
&&(t.rule_min>0)
&&(dteFrom>=t.rule\u start&&dtefroma.type\u id==3059)
.OrderBy(r=>r.pri);

如果
t.RulesApply
非法(即未编译),则将其替换为指向
Rules
对象的
RulesApply
对象上导航属性的正确引用。

如果在实体之间设置了导航属性,则可以从一个导航到另一个:

//This gets the RulesApply object
var rulesapply = db.RulesApply.Single(x=> x.type_id == 3059);

//This gets all Rules connected to the rulesapply object through its navigational property
var rules = rulesapply.Rules;

//You can use LINQ to further refine what you want
rules = rules.Where( x=> /* and so on...*/ );

您可以将这些语句堆叠在一行中,我只是出于可读性目的将它们拆分:)

谢谢-intellisense for t.RulesApply。没有任何属性,只是计数、顺序、第一等等。正确引用导航属性是什么意思?数据库表很旧,并且没有主键,因此将[key,column…]添加到可规则的表中。再次感谢你,马克
//This gets the RulesApply object
var rulesapply = db.RulesApply.Single(x=> x.type_id == 3059);

//This gets all Rules connected to the rulesapply object through its navigational property
var rules = rulesapply.Rules;

//You can use LINQ to further refine what you want
rules = rules.Where( x=> /* and so on...*/ );