using IF statements in Visual Studio 2010 - visual-studio-2010

I'm very new to Visual Studio, and am using 2010 right now.
I'm getting stuck in the Expression Builder when setting variables.
I have 2 vars #[User::Month] and #[User::Year]
#[User::Month] = MONTH( GETDATE() ) - 1
Now I need to set year to be current year unless #Month = 1, then be current year - 1. I just can't figure out how to use if statements in the Expression Builder.
I've tried:
If #[User::Month] == 1 Then
YEAR( GETDATE() ) - 1
Else
YEAR( GETDATE() )
EndIf
I've tried searching, but can't find any assistance on using the Expression Builder.

You are not currently setting anything in your if statement. The correct if statement would be:
If #[User::Month] == 1 Then
#[User::Year]=YEAR( GETDATE() ) - 1
Else
#[User::Year]=YEAR( GETDATE() )
EndIf

I ended up finding the answer here
#[User::Year]=(MONTH(GETDATE())==1? YEAR(GETDATE())-1 : YEAR(GETDATE() ))

Related

DolphinDB Calculate the time difference between TIMESTAMP and DATETIME?

I tried the script in DolphinDB:
t =table(
array(DATETIME, 0) as MDTime,
array(TIMESTAMP, 0) as ReceivedTime
)
share t as testShareTable;
select * from testShareTable;
select avg(ReceivedTime - MDTime) as DelayTime from testShareTable;
The table is
But the output is not as expected
Any suggestions to modify the code? Btw, I'd like to add 8 hours to ReceivedTime. How can I obtain the result?
Modified the last line of code:
select avg(temporalAdd(ReceivedTime,8H) - timestamp(MDTime)) as DelayTime from testShareTable;

Linq DateTime comparison not working

I have the following code:
DateTime timeStamp = Convert.ToDateTime(Request.QueryString["TimeStamp"]);
var result = (from rs in db.VRec
where
rs.TimeStamp == timeStamp &&
rs.Fixure == wFixture
select rs).ToList();
The result shows 0 even though the correct timeStamp is passed.
If I remove the part where I do the TimeStamp comparison:
rs.TimeStamp == timeStamp
The code works fine.
Any idea on why the datetime comparison may not be working?
DateTime has a pretty fine resolution - likely you are comparing timestamps that only differ in milliseconds, which will fail. You probably want something like:
DateTime now = DateTime.Now;
DateTime then = now.Add(TimeSpan.FromMilliseconds(1));
const int EPSILON_MS = 10;
if(now.Subtract(then).TotalMilliseconds < EPSILON_MS)
{
Console.WriteLine("More or less equal!");
}
Linq converts DateTime arguments to DateTime2 in the sql query executed.
That is, when you do the comparison the actual sql executed will compare a DateTime to a DateTime2. This comparison will "cast" the DateTime to a DateTime2 and the millisecond part will be expanded to a greater resolution (in an odd way in my opinion, please enlighten me).
Try to execute the following sql:
declare #d1 datetime = '2016-08-24 06:53:01.383'
declare #d2 datetime2 = '2016-08-24 06:53:01.383'
declare #d3 datetime2 = #d1
select #d1 as 'd1', #d2 'd2', #d3 'converted'
select (case when (#d1 = #d2) then 'True' else 'False' end) as 'Equal',
(case when (#d1 > #d2) then 'True' else 'False' end) as 'd1 greatest'
From the question, I do not know if you want to compare the date with time or only the date part. If you only want to compare date then following would work
var result = (from rs in db.VRec
where
rs.TimeStamp.Date == timeStamp.Date &&
rs.Fixure == wFixture
select rs).ToList();
Since you are using some reference to db, it gives me a feeling that you are fetching your records from database (which ORM you are using is not obvious from the question or tags). Assuming that you are using Entity framework the above query will fail with exception that .Date has no direct translation to sql. If so you can rewrite the query as following to make it work.
var result = (from rs in db.VRec
where
rs.TimeStamp.Day == timeStamp.Day &&
rs.TimeStamp.Month == timeStamp.Month &&
rs.TimeStamp.Year == timeStamp.Year &&
rs.Fixure == wFixture
select rs).ToList();
The benefit of this approach is that you can compare properties to arbitrary deep level i.e you can compare Hours, Minutes,Seconds etc. in your query. The second query is tested in Entity framework 5.

Parenthesis Expressions in LINQ to SQL

If I want to generate the query (month(created) = 1 and year(created) = 2010) or (month(modified) = 1 and year(modified) = 2010) with linq, how would I go about it?
I have o.Created.Value.Month == month && o.Created.Value.Year == year. If I do (o.Created.Value.Month == month && o.Created.Value.Year == year) || (o.Modified.Value.Month == month && o.Modified.Value.Year == year) wouldn't the parenthesis just be ignored?
No, the parentheses won't be ignored by LINQ - they're important to indicate the logic. They're effectively present in the expression tree, in that you'll end up with an "OR" expression with two subexpressions each of which is an "AND" expression.
The query you've given should be fine - have you tried it, and checked the resulting SQL?

How do I check if a SQL Server 2005 TEXT column is not null or empty using LINQ To Entities?

I'm new to LINQ and I'm trying to check whether a TEXT column is null or empty (as String.IsNullOrEmpty).
from c in ...
...
select new
{
c.Id,
HasBio = !String.IsNullOrEmpty(c.bio)
}
Trying to use the above query produces an SqlException:
Argument data type text is invalid for argument 1 of len function.
The SQL generated is similar to the following:
CASE WHEN ( NOT (([Extent2].[bio] IS NULL) OR (( CAST(LEN([Extent2].[bio]) AS int)) = 0))) THEN cast(1 as bit) WHEN (([Extent2].[bio] IS NULL) OR (( CAST(LEN([Extent2].[bio]) AS int)) = 0)) THEN cast(0 as bit) END AS [C1]
LEN is not applicable to TEXT columns. I know DATALENGTH should be used for them...
How can I force LINQ to produce such thing? Or any other workaround to test if a text column is null or empty???
Thanks!
Update
I came up with this
HasBio = c.bio.Substring(0, 1).Length > 0
but it's a little bit ugly though, any other options?
Well I've decided to convert the TEXT columns to VARCHAR(MAX) taking in mind the following article.
http://geekswithblogs.net/johnsPerfBlog/archive/2008/04/16/ntext-vs-nvarcharmax-in-sql-2005.aspx
powerCircuitList.Where(t => t.textProperty!= null && t.textProperty!= "")

compare the dates with linq

I have this code:
i.SpesaVitto = db.TDP_NotaSpeseSezB.Where(p => p.GiornoFine == null &&
((DateTime)p.Giorno).Date == ((DateTime)i.Data).Date &&
p.MissioneID == missioneID).Sum(p => p.Costo);
If i launch it i obtain this error:
The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
how can i compare the datetimes field without the hours part
thanks
Well this is certainly not the most efficient way to do it, but you can try something like:
i.SpesaVitto = db.TDP_NotaSpeseSezB.Where(p => p.GiornoFine == null &&
(new DateTime((p.Giorno as DateTime).Year, (p.Giorno as DateTime).Month, (p.Giorno as DateTime).Day) == (new DateTime((i.Data as DateTime).Year, (i.Data as DateTime).Month, (i.Data as DateTime).Day) &&
p.MissioneID == missioneID).Sum(p => p.Costo);
Depending on the requirements I would probably implement something cleaner and faster but it would depend on how the dates are stored in your database.
Check out this question it looks like the syntax error may be in your SQL, not in your LINQ. The asker in that question had written:
SQL ------
Select
EmployeeNameColumn
From
EmployeeTable
WHERE StartDateColumn.Date <= GETDATE() //Today
SQL ------
When they should have written:
SQL ------
Select
EmployeeNameColumn
From
EmployeeTable
WHERE StartDateColumn <= GETDATE() //Today
SQL -
solution for your problem will be the "SqlFunctions class and DateDiff" http://msdn.microsoft.com/en-us/library/system.data.objects.sqlclient.sqlfunctions.aspx

Resources