Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

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

0
locdang
Asked:
locdang
  • 3
  • 2
1 Solution
 
locdangAuthor Commented:
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

0
 
Fernando SotoRetiredCommented:
Hi locdang;

The code snippet below will do what you want. Note that the p.foo, is the column in the database and in the select clause, Foo = p.foo, where Foo is the property in the Something class and p.foo is the column in the database. In side the { } in the select statement you will need to individually fill the properties of the class that you want fill from the database. In the code snippet only one property is filled in the following line of code two are filled.

select new Something { Foo = p.foo, Bar = p.bar };

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

Open in new window

0
 
locdangAuthor Commented:
Perfect! Exactly what i needed!
0
 
locdangAuthor Commented:
Thank you very much!
0
 
Fernando SotoRetiredCommented:
Not a problem, glad I was able to help.  ;=)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now