linq accessing ui control values and sending results to backgroundworker [modified]
-
I have a linq query accessing ui control values and sending results to backgroundworker. Here is the goal.
// query accessing ui controls var crossjoin\_forumquestion = from businessid in new List<Int32>() { 1, 2, 3, 4, 5 } from yearid in new List<Int32>() { }.DefaultIfEmpty() select new { BusinessID = businessid, YearID = yearid }; // background worker will use the results as follows and do bulk inserts in db crossjoin\_forumquestion.Count(); foreach (var row in crossjoin\_forumquestion) { }
if i'm going be sending data outside the scope of the function it needs to be typed so. i created CrossJoinResult class, modified var to IEnumerable<CrossJoinResult> crossjoin_forumquestion = and changed select new to select new CrossJoinResult. however it looks like dowork event is throwing exception that query is outside the ui thread. sounds like the linq query is lazy loading. I need it to be fetched before i send the IEnumerable off to my dowork. my current fix is:
AsyncData threaddata = new AsyncData(); threaddata.CrossJoinResult = new List<CrossJoinResult>(crossjoin\_forumquestion);
//one of the overloads of a list takes in IEnumerable which extracts the data out the linq query.
//i would like the proper solution in telling linq to give me the result set now.thanks, -lm
modified on Thursday, October 2, 2008 10:29 PM
-
I have a linq query accessing ui control values and sending results to backgroundworker. Here is the goal.
// query accessing ui controls var crossjoin\_forumquestion = from businessid in new List<Int32>() { 1, 2, 3, 4, 5 } from yearid in new List<Int32>() { }.DefaultIfEmpty() select new { BusinessID = businessid, YearID = yearid }; // background worker will use the results as follows and do bulk inserts in db crossjoin\_forumquestion.Count(); foreach (var row in crossjoin\_forumquestion) { }
if i'm going be sending data outside the scope of the function it needs to be typed so. i created CrossJoinResult class, modified var to IEnumerable<CrossJoinResult> crossjoin_forumquestion = and changed select new to select new CrossJoinResult. however it looks like dowork event is throwing exception that query is outside the ui thread. sounds like the linq query is lazy loading. I need it to be fetched before i send the IEnumerable off to my dowork. my current fix is:
AsyncData threaddata = new AsyncData(); threaddata.CrossJoinResult = new List<CrossJoinResult>(crossjoin\_forumquestion);
//one of the overloads of a list takes in IEnumerable which extracts the data out the linq query.
//i would like the proper solution in telling linq to give me the result set now.thanks, -lm
modified on Thursday, October 2, 2008 10:29 PM
Linq uses something called deferred execution which basically means you don't get the data until you evaluate it (in whichever way you see fit). If you want to export this as a list, you can just use
crossjon_forumquestion.ToList();
to return a list.Deja View - the feeling that you've seen this post before.