Link to home
Start Free TrialLog in
Avatar of Joe Ruder
Joe RuderFlag for United States of America

asked on

using linq select with on a partial class

Hello;

I am using a linq statement to populate a datagrid as follows:

 Dim packages = From p In db.Packages _
                       Join d In db.details On p.PackageID Equals (d.MainID) _
                       Where p.statuscode = 1 And (d.InboundDate = datePicker.Value) And d.RecordType = 11 _
                       Select New OMGridData With {.name = d.ConsigneeName}

        PackagesDataView.DataSource = packages

Open in new window


I have named the columes using the dataPropertyName.

This works perfectly, I have created a test class OMGridData that has only one member.

However, instead of creating an entirely new class for this I would like to use a existing class: detailReportClass

detailReportClass has all the members I need and then some - that is the problem, it is creating a colume for every one of them.

How do I limit it?

In other words :

   Dim packages = From p In db.Packages _
                       Join d In db.details On p.PackageID Equals (d.MainID) _
                       Where p.statuscode = 1 And (d.InboundDate = datePicker.Value) And d.RecordType = 11 _
                       Select New detailReportClass With {.Consignee = d.ConsigneeName, .city = d.City}

Open in new window


Creates about 15 columes in my datagrid instead of just the two I want.

Any help would be great - I do this a lot and would rather now create redundant classes.

Thank you!
Avatar of nepaluz
nepaluz
Flag of United Kingdom of Great Britain and Northern Ireland image

It really depends on what you do with the data once it is in the datagrid. One option would be to set ONLY the columns you want to be visible AFTER setting the datasource, and the rest to invisible.
Avatar of Joe Ruder

ASKER

Is Just hiding them really the only way to do this?
I will be manipulating the grid data afterwards, setting different values in the rows and then importing them back into the database by stepping through each row.

If this is the best way, is there a way to set them all to not visible at once then just turn on the ones I want?

thank you...
Since you'll be using the data, then your best bet would be to simply show only the columns you want to show, that way, you preserve the structure of the data for saving back.

You have to iterate through them to set them to invisible AFTER setting the datasource, then "switch" on the ones you want
        For Each y As DataGridViewColumn In Me.DataGridView1.Columns
            y.Visible = False
        Next

Open in new window

Just one final time -- what other options do I have besides just hiding the columns?  It seems a waste of processing time and memory to import in a bunch of stuff I am not using (these can be large)....

Does anybody know a different way?

I do appreciate the help - I am just double checking.
the last and best resort would be to fine tune your linq statement to include ONLY the columns  data that you require.
*That* is what I am trying to do!  That was the entire purpose of the OP.

How do I do that?

In my linq statement I only select 2 fields, yet I am getting them all still.

Do I need to repost this question so somebody who knows how to do it will see it?  I am not trying to be cranky, however, after 5 times all you did was tell me that I should do what I was trying to get help to do in the first place.  I apologize if my OP was not clear.

Thank you,

Joe
ASKER CERTIFIED SOLUTION
Avatar of nepaluz
nepaluz
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
My "attidude" did not stink - the experts 'answer' was to simply re-word my original question and tell me to do that.  However, I did find the answer due to some hints buried in the midst of his masive whining.