Link to home
Start Free TrialLog in
Avatar of locdang
locdang

asked on

Help with LINQ to sql

Hi everyone,

I need some help with being able to use LINQ  to fill a List<> of my own object type.

the error i am receiving is "Cannot implicitly convert type 'System.Linq.IQueryable<LINQExperiment.tblSomething>' to 'System.Collections.Generic.List<LINQExperiment.Something>'. An explicit conversion exists (are you missing a cast?)"

here is my example below, hopefully it will show what i wish to achieve

Thanks,

Xavier.
private void button1_Click(object sender, EventArgs e)
        {
            DemoDBDataContext demoDB = new DemoDBDataContext ();
            var res = from p in demoDB.Somethings where p.Foo == "abc" select p;
            List<Something> somethingCollection = res;
        }


class Something
    {
        public String Foo
        {
            get;
            set;
        }
        public Int32 Bar
        {
            get;
            set;
        }
    }

Open in new window

Avatar of locdang
locdang

ASKER

I have tried creating an explicit cast method in my Something class however whilst this works, i still cannot create a list without looping through each item in the LINQ result and manually adding each result into a new Something object, so this really isnt the solution...
public static explicit operator Something(tblSomething s)
        {
            Something so = new Something();
            so.Foo = s.Foo;
            so.Bar = (int)s.Bar;
            return so;
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America 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
Avatar of locdang

ASKER

Perfect! Exactly what i needed!
Avatar of locdang

ASKER

Thank you very much!
Not a problem, glad I was able to help.  ;=)