We help IT Professionals succeed at work.

Linq To sql problem

399 Views
Last Modified: 2012-06-27
Hello EE,

I have this :

        Dim q = (From m In myTableValue
                Select m.Field(Of String)(myColumn)).Distinct().ToList()

myTableValue is a DataTable.

I need to create a Dictionnary from that LINQ. I know a dictionnary needs two elements. (key and a value) and I can duplicate the same field on both elements? and how do I get that linq to be a Dictionnary (string, string) ?
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
Try this:

Dim q = (From m In myTableValue
         Select m.Field(Of String)(myColumn)).Distinct()
                                             .ToDictionary(Function (f) f.ToString(),
                                                           Function (f) f.ToString())

Open in new window

Author

Commented:
so q is now a dictionnary ?

I could do like :


        Dim r = _
    (From m In q
     Select ID = m.Key _
          ,     Desc = m.Value).ToList()

here I would have error that q is not Queryable
what I want to accomplish actually is that I want to Alias the name in the Dictionnary to be ID and Desc  and then after I would to .ToList() so that my ListBox datasource be filled with that list !
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
Wouldn't it be easier to "alias" the stuff that comes back from the DB?

e.g.

Dim q = (From m In myTableValue _
         Select New With _
         { _
            .ID = m.Field(Of String)(myColumn)), _
            .Desc = m.Field(Of String)(myColumn)), _
         }).ToList()

Open in new window


Now you would have a list of anonymous objects, each having two properties:  ID and Desc.
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
so q is now a dictionnary ?
Oh, and the answer to that question is yes, q would be a Dictionary. However, this not the case in my last post.

Author

Commented:
Alright, jsut before giving points,  lets say mycolumn  returns integer and I want it Of String

without modifying the Store Procedure, is it possible to Cast in LINQ ?

something like  Cstr(m.Field(Of Integer)     you know ?
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
You can. As long as the field wasn't null, you could also do ToString.

Dim q = (From m In myTableValue _
         Select New With _
         { _
            .ID = CStr(m.Field(Of Integer)(myColumn))), _
            .Desc = m.Field(Of String)(myColumn)), _
         }).ToList()

Open in new window


--OR--

Dim q = (From m In myTableValue _
         Select New With _
         { _
            .ID = m.Field(Of Integer)(myColumn)).ToString(), _
            .Desc = m.Field(Of String)(myColumn)), _
         }).ToList()

Open in new window

Author

Commented:
By the way I jsut tried all the code,

but Key and Value  are  "System.Data.DataRow"

looks liek we are missing something ?
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
Remember that with anonymous types (and Dim foo), the compiler infers the type of the object based on the type that occurs on the right side of the = symbol. In the above, since you cast the field's value to a String (either via CStr or ToString), the resulting object's property will be of type String. If you did not include the cast, then the resulting object would be of type Integer since the field's type is Integer. (This is regarding the type as it occurs in your VB code, not necessarily the type as it occurs in the DB.)
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
By the way I jsut tried all the code,

but Key and Value  are  "System.Data.DataRow"

looks liek we are missing something ?
Which post?

Author

Commented:
Dim q = (From m In myTableValue
         Select m.Field(Of String)(myColumn)).Distinct()
                                             .ToDictionary(Function (f) f.ToString(),
                                                           Function (f) f.ToString()).ToList()


        listCol.DataSource = q


q  count is 1  and Key is System.Data.DataRow    Value also ..
I would like to be as:    { Key = "1",  Value="my description" }

so after I could do:   listbox1.Items(0).Key   or  .Value  to retrieve info
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Would it not be simpler if you add a CopyToDataTable extension method and get a datatable to bind to list?

http://msdn.microsoft.com/en-us/library/bb386921.aspx

Author

Commented:
CodeCruiser, yea maybe but all the code is going into that direction now
if I fix that in the next 30 secs everything works.


otherwisde I need to change alot of things

if its not possible I will do it but.. if you guys have a solution... :)
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.