使用linq在nhibernate中创建查询

时间:2011-05-13 13:11:52

标签: nhibernate fluent-nhibernate

我想创建像

这样的东西

SELECT * FROM dbo.localconveyance_details WHERE voucherNo IN(SELECT voucherNo FROM dbo.localconveyance_master WHERE emp_code ='48'

在流利的nhibernate中使用linq

试过这样的事情

IList<LocalConveyanceDetails> detailslist = session.Query<LocalConveyanceDetails>()
                    .Where(x => x.LocalConveyanceMaster.emp_code == e_id).ToList();

但它不起作用......有人能说出实际的查询是什么吗?

更新:

我使用的实体是:

 public class LocalConveyanceMaster
{
    public virtual String voucherNo { get; set; }
    public virtual DateTime voucher_date { get; set; }
    public virtual String emp_code { get; set; }
    public virtual String emp_name { get; set; }
    public virtual String project_id { get; set; }
    public virtual DateTime submitDate { get; set; }
    public virtual String to_be_approved_by { get; set; }
    public virtual String created_by { get; set; }
    public virtual Decimal conveyance_total { get; set; }
    public virtual Decimal approved_amount { get; set; }
    public virtual String approved { get; set; }

    public virtual ProjectMaster Project { get; set; }
    public virtual ICollection<LocalConveyanceDetails> LocalConveyanceDetails { get; set; }
}
public class LocalConveyanceDetails
{
    public virtual String LcDetailsId { get; set; }
    public virtual String voucherNo { get; set; }
    public virtual String serialNo { get; set; }
    public virtual String From_Project_Id { get; set; }
    public virtual String To_Project_Id { get; set; }
    public virtual DateTime particularsDate { get; set; }
    public virtual Decimal particularsAmount { get; set; }
    public virtual String particulars { get; set; }

    public virtual LocalConveyanceMaster LocalConveyanceMaster { get; set; }
}

和映射是:

 public LocalConveyanceMap()
    {
        Table("localconveyance_master");
        Id(x => x.voucherNo).Column("voucherNo");
        Map(x => x.voucher_date);
        Map(x => x.emp_code);
        Map(x => x.emp_name);
        Map(x => x.project_id);
        Map(x => x.submitDate);
        Map(x => x.to_be_approved_by);
        Map(x => x.created_by);
        Map(x => x.conveyance_total);
        Map(x => x.approved_amount);
        Map(x => x.approved);

        References(x => x.Project)
            .Column("project_id")
            .ForeignKey("project_id");

        HasMany(x => x.LocalConveyanceDetails)
            .KeyColumn("voucherno").AsSet();

    }
 public LocalConveyanceDetailsMap()
    {
        Table("Localconveyance_details");
        Id(x => x.LcDetailsId).Column("LcDetailsId");
        Map(x => x.voucherNo);
        Map(x => x.serialNo);
        Map(x => x.From_Project_Id);
        Map(x => x.To_Project_Id);
        Map(x => x.particularsDate);
        Map(x => x.particularsAmount);
        Map(x => x.particulars);


        References(x => x.LocalConveyanceMaster)
            .PropertyRef(x => x.voucherNo).Column("voucherno")
            .ForeignKey("voucherno");

    }

我得到的错误是:

异常: {“执行LoadByUniqueKey时出错[SQL:SQL不可用]”}

InnerException: {“字典中没有给定的密钥。”}

1 个答案:

答案 0 :(得分:0)

当一个或多个类的成员是其他类[关系]时发生的。 我有同样的问题,我删除了这些成员,并以这种形式为每个成员添加新成员:

public virtual int? [相关课程] id {get; set;}

exmaple:

public virtual int? PersonId {get; set;}