4 Replies - 940 Views - Last Post: 16 November 2011 - 09:50 PM Rate Topic: -----

#1 nathanh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-August 11

IQueryable - Datagridview Databinding

Posted 16 November 2011 - 06:37 AM

I am using methods that return an IQueryable type to add user specified search parameters. I have an extended search method which creates a copy of my base search query method and extends it with further parameters. I can then assign the returned IQueryable type as the datasource for my datagridview.

The problem is that the returned object <asset> contains several foreign keys that are absolutely pointless to the end user to see. What would be the best practice for changing the displayed values to another column in the referenced table?

        private void bt_search_Click(object sender, EventArgs e)
        {
            assetBindingSource.DataSource = ExtendedSearch();
            dataGridView.DataSource = assetBindingSource;
        }
        private IQueryable<asset> BaseSearch()
        {
            var context = DBEntities.Get();
            
            var results = from a in context.assets
                          select a;
            return results;
        }

        private IQueryable<asset> ExtendedSearch()
        {
            var extendedSearch = BaseSearch();
            extendedSearch = extendedSearch.Where(x => x.assetType.typeCtgry.ctgry_ofc == parentForm.selectedOffice.ofc_id);

            //Other paramaters to add
            /*if (!String.IsNullOrEmpty(somestring))
                extendedSearch = extendedSearch.Where(x => x.desc.Contains(sometb.Text.Trim()));*/

            return extendedSearch;
        }



I know I could loop through the results assigning the values to the grid however, I'm sure there must be a more efficient way to handle it.
          IQueryable<asset> results = ExtendedSearch();
            foreach (asset a in results)
            {
                //manually adding object attributes to the gridview
                a.id
                a.desc
                a.assetType.txt //foreign key

            }



Is This A Good Question/Topic? 0
  • +

Replies To: IQueryable - Datagridview Databinding

#2 tsackey  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 11-March 10

Re: IQueryable - Datagridview Databinding

Posted 16 November 2011 - 12:46 PM

You could select just the columns u need in your linq query i.e.
 var results = from a in context.assets
                          select a.column1,a.column2;


Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3803
  • View blog
  • Posts: 6,410
  • Joined: 08-June 10

Re: IQueryable - Datagridview Databinding

Posted 16 November 2011 - 01:21 PM

No you can't. At least, not using that syntax. You can't treat LINQ like SQL.

If you want to select multiple columns, you'll have to select it into a new object:

var results = from a in context.assets
              select new {Id = a.id, Desc = a.desc, /*etc...*/};

Was This Post Helpful? 1
  • +
  • -

#4 tsackey  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 11-March 10

Re: IQueryable - Datagridview Databinding

Posted 16 November 2011 - 02:43 PM

thanks for the correction Curtis.

View PostCurtis Rutland, on 16 November 2011 - 03:21 PM, said:

No you can't. At least, not using that syntax. You can't treat LINQ like SQL.

If you want to select multiple columns, you'll have to select it into a new object:

var results = from a in context.assets
              select new {Id = a.id, Desc = a.desc, /*etc...*/};

Was This Post Helpful? 1
  • +
  • -

#5 nathanh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-August 11

Re: IQueryable - Datagridview Databinding

Posted 16 November 2011 - 09:50 PM

Thank you tsackey and Curtis! That's exactly what I needed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1