使用 linq 在 nhibernate 中的查询中创建

发布于 2024-11-06 20:44:20 字数 3288 浏览 0 评论 0原文

我想创建类似

SELECT * FROM dbo.localconcaveance_details WHERE 的 内容 oucherNo IN (SELECToucherNo 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:{“给定的键不存在”字典里有。”}

i want to create something like

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

using linq in fluent nhibernate

tried something like this

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

but its not working ... can someone tell what would be the actual query ?

Update :

the Entities which i have used are :

 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; }
}

and the mappings are :

 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");

    }

the error which i am getting is :

Exception : {"Error performing LoadByUniqueKey[SQL: SQL not available]"}

InnerException : {"The given key was not present in the dictionary."}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

惜醉颜 2024-11-13 20:44:20

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

公共虚拟整数? [相关类]id {get;set;}

exmaple :

public virtual int? PersonId {获取;设置;}

that's occurred when the one or more member of class is kind of other class [relationship].
i have a same problem and i remove that members and add new member for each of them in this form :

public virtual int? [related class]id {get;set;}

exmaple :

public virtual int? PersonId {get;set;}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文