tags:

views:

26

answers:

1

I have a Linq query and I want to pass the ouput (userid) to another form for further processing.

 var userid = from auser in allusers.Users where auser.Username == nameString
        select new { id = auser.UserId };

so only 'UserId' is stored in variable 'userid' and I want to use this value in another form. Is there any way we can do this.

+4  A: 

If you want to return the result of a query you cannot use anonymous types. You must create a concrete class. In your specific case you could just use an int? like this:

int? userid = (from auser in allusers.Users
               where auser.Username == nameString
               select auser.UserId).SingleOrDefault();
Mark Byers
@Mark Thanks.can you also refer me to some website or some code which I can refer to since I am new to this.. what I was trying is to give the output of this var..to some varibale, but i guess we cannot do that coz there's no way we can convert it to appropiate type...it it true
Ani
@Ani: I added some code.
Mark Byers
@Mark now how can I pass this value in userid to another form sir.Thanks for helping me out.
Ani
@Ani: If you only want to pass a single value rather than a query, use Single to get the value and pass that value using the same technique as you would to pass any other value between forms, e.g. see this question: http://stackoverflow.com/questions/1665533/communicate-between-two-windows-forms-in-c - See my updated code.
Mark Byers
@Mark I got the desired output...I am really thankful to you.
Ani