Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Adding extra conditions
Message
From
25/06/2013 11:56:30
 
General information
Forum:
ASP.NET
Category:
LINQ
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01577034
Message ID:
01577085
Views:
35
This message has been marked as a message which has helped to the initial question of the thread.
Likes (1)
>Hi everybody,
>
>I have the following code
>
>
> EnumerableRowCollection<DataRow> query;
>                  if (checkType && checkCategory)
>                     query = from c in dtMax4SaleLimits.AsEnumerable()
>                             where ((2 == c.Field<Byte>("type") && lsn_cat == c.Field<String>("lsn_cat"))
>                             || (3 == c.Field<Byte>("type") && lessontype == c.Field<Int32>("lessontype")))
>                             && (c.Field<DateTime?>("start_time") < ending_time &&
>                                 c.Field<DateTime?>("end_time") > begin_time)
>                             select c;
>                  else
>                  {
>                     if (checkCategory)
>                        query = from c in dtMax4SaleLimits.AsEnumerable()
>                                where (2 == c.Field<Byte>("type") && lsn_cat == c.Field<String>("lsn_cat"))
>                                && (c.Field<DateTime?>("start_time") < ending_time &&
>                                 c.Field<DateTime?>("end_time") > begin_time)
>                                select c;
>                     else
>                        query = from c in dtMax4SaleLimits.AsEnumerable()
>                                where (3 == c.Field<Byte>("type") && lessontype == c.Field<Int32>("lessontype"))
>                                && (c.Field<DateTime?>("start_time") < ending_time &&
>                                 c.Field<DateTime?>("end_time") > begin_time)
>                                select c;
>                  }
>
>                  Int32 recordsCount = query.Count();
>                  if (0 == recordsCount)
>
>This code in executed in the loop for 10 different time values. However, I noticed that the lesson type and lsn_cat is not matching, therefore every time I'll get 0 rows. So, I am thinking I may try to execute my query first for the conditions without time checks and if it returns 0 rows, there is no need for the loop at all.
>
>The question is - how should I proceed - should I use two separate query variables - one for the main query without the extra date condition and another with the condition or there is a way to re-use the same query variable and add extra where condition?
>
>Thanks in advance.

1) You can use the LINQ method syntax to add additional where clauses to your query, and if you want to, reuse the query variable.
2) If you are only checking if records exist, use the Any method.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform