从SQL里面选择for循环

时间:2017-01-18 15:33:58

标签: sql for-loop razor foreach

我正试图从每个有值的日期得到数据库中的一些乘法值,就​​像这样!

var sumKG = 0;
DateTime nextDay;
//Just a random date for test purposes.
var inputDate = "2016-12-27";

for (int i = 1; i < 5; i++)
{

    var getData = "SELECT * FROM Test WHERE date = '" + inputDate + "'"; 

    foreach (var c in db.Query(getData))
    {
        //This is the calculation that I want.
        var total = c.kg * c.rep * c.sett;

        //These three rows are date related and add 1 day to
        //inputDate which is used above in the SQL query line.
        DateTime thisDay = c.date;
        nextDay = thisDay.AddDays(1);
        inputDate = nextDay.ToString("dd/MM/yy");

        //This gives the total of the calculations done in each foreach loop.
        sumKG += total;
    }
    @sumKG <br />
}

所以我想从这里得到的是,当foreach完成了数据库中每一行的日期“2016-12-27”之后,它应该更改{{1} }到“2016-12-28”并再次为与该新日期约会的每一行运行inputDate

但是,虽然sql查询在foreach内,但这不起作用,我收到错误:

for loop

我不知道该怎么办?因为在我看来,sql查询必须在The data type is not valid for the boolean operation.内,否则它将无法获得for loop的新值?

有什么建议吗? (不是MVC项目)

1 个答案:

答案 0 :(得分:1)

你可以尝试下面的事情然后我应该工作..

inputDate = nextDay.ToString("yyyy-MM-dd");