0

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?

Nicolas Henneaux
  • 11,507
  • 11
  • 57
  • 82
Ani
  • 598
  • 5
  • 13
  • 29

2 Answers2

4

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
  • 811,555
  • 193
  • 1,581
  • 1,452
  • @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 Apr 23 '10 at 21:58
  • now how can I pass this value in userid to another form sir. Thanks for helping me out. – Ani Apr 23 '10 at 22:13
  • @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 Apr 23 '10 at 22:21
0
    IEnumerable <TableName> result = from x in dataBase.TableName
                                     select x;     
    methodName(result);



    private void methodName (IEnumerable<TableName> result) 
    {
     codes.....
    }
Daniel
  • 3,322
  • 5
  • 30
  • 40