Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Getting same data table after LINQ query
Message
 
 
À
11/06/2014 15:02:11
Information générale
Forum:
ASP.NET
Catégorie:
LINQ
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01601612
Message ID:
01601732
Vues:
21
>>>>Hi Rob,
>>>>
>>>>This doesn't seem to work as is
>>>>
>>>>
>>>>  if (resourceEvent)                              
>>>>                  {
>>>>                      // Remove instructors who don't have anything planned
>>>>                      var query = dtInstructors.AsEnumerable().Where(c => dtAllScheduled.AsEnumerable().Any(b =>  1 == b.Field<Byte>("layer") 
>>>>                                          && true==b.Field<Boolean>("schdassign") && c.Field<String>("instr_id")== b.Field<String>("resource_id")
>>>>                                          ));
>>>>                      query.CopyToDataTable(dtInstructors, LoadOption.OverwriteChanges);
>>>>                  }
>>>>
>>>>My dtInstructors has rows but dtAllScheduled does not.
>>>>
>>>>After the query I am supposed to get 0 rows in the dtInstructors but it seems to be the same table as it was before the query. Do you see how can I adjust the code to get the query "overwrite" my original table?
>>>>
>>>>Thanks.
>>>
>>>Is it throwing an InvalidOperationException that is being caught elsewhere? See the conditions for that exception at http://msdn.microsoft.com/en-us/library/bb359707(v=vs.110).aspx
>>
>>Update.
>>
>>I figured this out with the following code:
>>
>>
>>               DataTable dtInstructorsAll = this.GetDataSet().Tables[0];
>>               DataTable dtInstructors;
>>
>>               if (dtInstructorsAll.Rows.Count > 0)
>>               {
>>                  DateTime minDate = Functions.Smallest<DateTime>((DateTime)bRow.StartTime, preferenceStartTime);
>>                  DateTime maxDate = Functions.Largest<DateTime>((DateTime)bRow.EndTime, preferenceEndTime);
>>
>>                  DateRange range = new DateRange(minDate, maxDate);
>>
>>                  // GetPvtSortedResources doesn't not use instructor type where condition while GetPvtSortedSchedule does use it
>>                  DataTable dtAllScheduled = this.GetAllScheduled(sqlCommand, range, (resourceOnly) ? "" : instructorTypeWhere, resourceEvent);
>>                  Logging.LogFormat(4, "Number of eligible bookings returned in csrAllSched: {0}", dtAllScheduled.Rows.Count);
>>
>>                  if (resourceEvent)                              
>>                  {
>>                      // Remove instructors who don't have anything planned
>>                      dtInstructors = dtInstructorsAll.Clone();
>>                      var query = dtInstructorsAll.AsEnumerable().Where(c => dtAllScheduled.AsEnumerable().Any(b =>  1 == b.Field<Byte>("layer") 
>>                                          && true==b.Field<Boolean>("schdassign") && c.Field<String>("instr_id")== b.Field<String>("resource_id")
>>                                          ));
>>                      query.CopyToDataTable(dtInstructors, LoadOption.OverwriteChanges);
>>                  }
>>                      else
>>                  {
>>                      dtInstructors = dtInstructorsAll.Copy();
>>                  }
>>
>>
>>
>>So, I originally create a different table and then either copy or clone and CopyToDataTable depending on my condition.
>>
>>Does it sound like an OK solution?
>
>You should be checking query.Any() prior to doing the CopyToDataTable. CopyToDataTable is supposed to be throwing an InvalidOperationException if the query is empty.

So, do you suggest to add a check for query.Count == 0 prior to CopyToDataTable and in the case of 0 just bail out? May be I would not need the clone and copy at all, e.g. it would work as is in case my query returns rows?
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform