我尝试打印销售发票时出现问题

时间:2019-12-23 13:24:29

标签: c# sql-server crystal-reports entity-framework-6

private void button1_Click(object sender, EventArgs e)
{
    hesham2020Entities2 db = new hesham2020Entities2();
    string u;
    u = textBox1.Text.Trim();
    var y = from v in db.inv_detail
            from s in db.invoice_head 
            where s.invno==u
            select new
                {v.unit,v.qty,v.p_no,v.description,v.price,v.tot_price,s.invno,s.customer_id,s.inv_dat,s.po_no,s.total,s.currency};
    CrystalReport6 crt = new CrystalReport6();
    crt.SetDataSource(y);
    crv9.Refresh();
    crt.SetParameterValue(0, comboBox2.SelectedItem);
    crt.SetParameterValue(1, comboBox3.SelectedItem);
    //crt.SetParameterValue(2, textBox1.Text);
    crv9.ReportSource = crt;
    crv9.Refresh();
}

当我尝试打印此发票时,如果发票明细包含3行,则它打印9行, 这意味着每行打印3次,但是当发票明细包含1行时,它打印1行。我不知道错误的原因

2 个答案:

答案 0 :(得分:1)

您缺少inv_detail和invoice_head之间的联接。 基本上,您正在做笛卡尔积,您应该尝试类似的事情:

from db.invoice_head 
join db.inv_detail
on [...]

答案 1 :(得分:0)

我认为以下代码可以为您提供帮助,也许您忘记了正确使用join。

我将Id属性用于Join

var y = (from v in db.inv_detail
                 join  s in db.invoice_head on v.HeaderId equals s.Id
            where s.Id == u
                 select new
            {
                v.Id
            }).ToList();

var query = db.inv_detail
            .Join(db.invoice_head, 
                post => post.HeaderId,       
                meta => meta.Id,  
                (post, meta) => new { Post = post, Meta = meta }) 
            .Where(postAndMeta => postAndMeta.Post.Id == u);