如何使用已知数字的KnapSack问题获取数据行? [英] How to fetch datarows using a KnapSack Problem with known numbers ?

查看:49
本文介绍了如何使用已知数字的KnapSack问题获取数据行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个应用程序,它将通过从数据库中提取来生成发票SQL。

这就像一个KnapSack问题。这里看看我做了什么:

 DataTable dt = new DataTable(); 
dt.Columns.AddRange(new DataColumn [5] {
new DataColumn(" ProductId",typeof(string)),
new DataColumn(" Product",typeof(string) ),
new DataColumn(" Price",typeof(int)),
new DataColumn(" Quantity",typeof(int)),
new DataColumn(" Total", typeof运算(INT))});
dt.Rows.Add(101,"Sun Glasses",200,5,1000);
dt.Rows.Add(102," Jeans",400,2,800);
dt.Rows.Add(103,"Trousers",300,3,900);
dt.Rows.Add(104," Shirts",550,2,11100);



所以这里你可以看我们有产品,产品价格,数量和总数(价格*数量)



So我的目标是例如



发票1我想在里面总价1 500



所以我需要的是我们如何从数据库中获取它以便它自动填充我需要的正确数量。



INVOICE 1:TOTAL 1 500 USD 

产品1:衬衫 - 550 -2 - 1100

Product2 :牛仔裤 - 400 - 1- 400

总计= 1500(1100 + 400)

它会自动删除数据库中的产品数量。





这就像一个KnapSack问题...

有可能吗?



谢谢。

解决方案

DataColumn有一个< a href ="https://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression(v=vs.110).aspx">
表达式属性,这个将执行每一行的总计。


您可以通过lambda获得发票总额,如下所示(在Visual Studio外部手写)


var tot = dt.AsEmerable()。其中​​(row => row.Field< in>(" PK")= intInvoiceNumer).Select(row => row.Field< int>(" TheExpressionColumnName"))。 ();


I am doing an app which will generate invoices by fetching from database SQL.
It is like a KnapSack Problem. Here see What I did :

 DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[5] {
                        new DataColumn("ProductId", typeof(string)),
                        new DataColumn("Product", typeof(string)),
                        new DataColumn("Price", typeof(int)),
                        new DataColumn("Quantity", typeof(int)),
                        new DataColumn("Total", typeof(int))});
dt.Rows.Add(101, "Sun Glasses", 200, 5, 1000);
dt.Rows.Add(102, "Jeans", 400, 2, 800);
dt.Rows.Add(103, "Trousers", 300, 3, 900);
dt.Rows.Add(104, "Shirts", 550, 2, 1100);

So here As you can see we have Product, Price of the product , the quantity and the total (Price * Quantity)

So my aim is for example

Invoice 1 I want inside it the total price of 1 500

so here my need is that how can we fetch from database so that it will automatically fill the right amount that I need.

INVOICE 1 : TOTAL 1 500 USD 
Product1 : Shirts - 550 -2 - 1100
Product2: Jeans - 400 - 1- 400
Total = 1500 (1100+400)
And it automatically removes the quantity of products taken in the database.


It is like a KnapSack Problem...
Is it possible ?

Thank you.

解决方案

A DataColumn has an Expression property, this will do each row's total.

You can get an invoice total via lambda something like the following (hand written outside of Visual Studio)

var tot = dt.AsEmerable().Where(row => row.Field<in>("PK") = intInvoiceNumer).Select(row => row.Field<int>("TheExpressionColumnName")).Sum();


这篇关于如何使用已知数字的KnapSack问题获取数据行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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