简体   繁体   English

在DataTable中过滤结果(date1和date2之间的日期列)

[英]Filtering results within a DataTable (date column between date1 and date2)

I am using c#, sqlce, datatable. 我正在使用c#,sqlce,datatable。 I select products with this query: 我用这个查询选择产品:

select price from products where productid=myproduct and mydate between startdate and finaldate and clientid=myclient

But it's going to take much time. 但这需要很长时间。 So I could use a datatable to store products: 所以我可以使用数据表来存储产品:

datatable prices = new datatable(); 
prices=this.returntable("select productid,startdate,finaldate,client from products")


 for(int i=0;i<allproductsihave;i++) {
    product.newprice=this.getmyprice(prices,product.client,product.id,product.date);
 }


 private decimal getmyprice(datatable products,string myclient, string myproduct, datetime mydate)
 {
   //how do I do at here the equivalent to first sql query?
   //for select a product from a datatable where my date is between 2 dates,
   //and client=myclient and productid=myproduct
   return Convert.Todecimal(datatable.prices[something]["price"].tostring());
 }

In this way I shouldn't connect to the database for each product query. 这样我就不应该为每个产品查询连接到数据库。 Is it posible? 它可以吗?

maybe doing convert.todatetime to startdate and finaldate? 也许做convert.todatetime到startdate和finaldate?

Sounds like you dont want to make multiple requests to the database because you are worried about making several connections, which will be slower? 听起来你不想对数据库发出多个请求,因为你担心要建立几个连接,这会更慢? If you dont want to connect for every product, why not just use a "where product in ('xxx', 'yyy')" instead? 如果你不想连接每个产品,为什么不直接使用“'('xxx','yyy')中的产品?” Then you have a single query call, and no changes on the database. 然后,您只有一个查询调用,并且数据库没有任何更改。 You can then process the results when you get them back. 然后,您可以在返回结果时处理结果。

Your boss should speak to @pilotcam. 你的老板应该跟@pilotcam说话。 He is right. 他是对的。 Its no quicker in c# than it would be on the database. 它在c#中没有比在数据库上更快。 :) :)

In fact, doing this in c# means you are getting information back (I assume over a network if its a remote database) which you would never use, so its probably slower, how much depends on how much data is in your database which wont be in your final results! 事实上,在c#中执行此操作意味着您将获得信息(我假设它是一个远程数据库,我假设它是永远不会使用的),所以它可能更慢,多少取决于您的数据库中有多少数据不会是在你的最终结果中!

The DataTable.Select() method accepts a string representing a "where" condition and returns an array of DataRow containing matching rows. DataTable.Select()方法接受表示“where”条件的字符串,并返回包含匹配行的DataRow数组。

Datarow[] products = prices.Select(string.Format("'{0}'>='{1}' AND '{0}'<='{2}'", mydate, startdate, finaldate));

Mind the date format!! 记住日期格式!! It must be sql compliant. 它必须是sql兼容的。

As much as I disagree with this method for reasons stated in comments, here's one possibility. 尽管我在评论中提到的原因不同意这种方法,但这是一种可能性。 The idea here is to check for the most obvious exclusions first, then work your way to the date later. 这里的想法是首先检查最明显的排除,然后按照你的方式到达日期。

    private decimal GetMyPrice(DataTable table, string client, string product, DateTime date)
    {
        foreach (DataRow row in table.Rows)
        {
            if (row["productid"] != product) continue;
            if (row["client"] != client) continue;
            if (Convert.ToDateTime(row["startdate"]) < date) continue;
            if (Convert.ToDateTime(row["finaldate"]) > date) continue;

            return Convert.ToDecimal(row["price"]); 
        }
        throw new KeyNotFoundException();               
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM