locked
how can i get single / collection value from Iqueryable RRS feed

  • Question

  • how can i get value from Iqueryable

    my method is :

           public IQueryable<User_Search> get_user_Search_by_usersearch_ID(decimal _usersearch_ID) 
            {
                return ObjectContext.User_Search
                    .Include("Address_Book")
                    .Include("Address_Book1")
                    .Include("Address_Book2")
                    .Include("Job_Seeker")
                    .Include("User_Search_Address_Book")
                    .Include("User_Search_Industry")
                    .Include("User_Search_Location")
                    .Where(lo=>lo.User_Search_ID==_usersearch_ID);
            }
    i want to get value from the returned value to pass it to control
     
    txtkeyword.Text = recdomain.Load(recdomain.get_user_Search_by_usersearch_IDQuery(65)).Entities.ToList()[0].key_word;

    but it does not woke
    is that any way to get 
    silngle value and 
    get collection from opservable collection property


     

    Thursday, December 23, 2010 7:38 AM

Answers

  • When you load data with your query, you usually have a LoadComplete event handler or Load callback function. In that function you can access your Loaded data.

    If you use DomainDataSource to load your data, you can have a DomainDataSource.LoadedData event:

    private void DomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
            {
                if (e.HasError)
                    MessageBox.Show(e.Error.Message);
                else if(!e.Cancelled)                            
                   YourDataObject data = e.Entities.FirstOrDefault(); // Here you can get the Single object out                         
            }

    If you use DomainContext.Load:

    context.Load(context.YourQuery(), lo=>

    {

         if(!lo.HasError)

         {

                YourDataObject data = lo.Entites.FirstOrDefault(); // Here you can get the Single object out 

         }

    }, null);

    Friday, December 24, 2010 5:42 PM

All replies

  • If you want Single object out of IQueryable, you can use FirstOrDefault or SingleOrDefault Linq function:

    YourObject o = e.Entities.FirstOrDefault();   // Returns the first one (or null if the list is emtpy)

    YourObject o = e.Entities.FirstOrDefault(a=>a.XXX == YYY); // Returns the first one (or null) matchs the query

    OR

    YourObject o = e.Entities.SingleOrDefault(); //Returns the only one

    YourObject o = e.Entities.SingleOrDefault(a=>a.XXX == YYY); //Returns the only one matches the query

    SingleOrDefault function returns the only one in the list matches the query. you would get an error when using SingleOrDefaul function if there are more than one in the list matches the query, So use FirstOrDefault is always safer than use SingleOrDefault.

    Thursday, December 23, 2010 8:16 AM
  • thanks for answer,

    but what (e) refer to

    and your method returned 1 value that is what i want but could you give me sample to get list of data 

    Friday, December 24, 2010 4:48 PM
  • When you load data with your query, you usually have a LoadComplete event handler or Load callback function. In that function you can access your Loaded data.

    If you use DomainDataSource to load your data, you can have a DomainDataSource.LoadedData event:

    private void DomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
            {
                if (e.HasError)
                    MessageBox.Show(e.Error.Message);
                else if(!e.Cancelled)                            
                   YourDataObject data = e.Entities.FirstOrDefault(); // Here you can get the Single object out                         
            }

    If you use DomainContext.Load:

    context.Load(context.YourQuery(), lo=>

    {

         if(!lo.HasError)

         {

                YourDataObject data = lo.Entites.FirstOrDefault(); // Here you can get the Single object out 

         }

    }, null);

    Friday, December 24, 2010 5:42 PM