?
Solved

Help with LINQ to sql

Posted on 2010-01-11
5
Medium Priority
?
291 Views
Last Modified: 2013-11-26
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
Comment
Question by:locdang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 1

Author Comment

by:locdang
ID: 26289468
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
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 26290252
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
 
LVL 1

Author Comment

by:locdang
ID: 26290291
Perfect! Exactly what i needed!
0
 
LVL 1

Author Closing Comment

by:locdang
ID: 31675864
Thank you very much!
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 26290324
Not a problem, glad I was able to help.  ;=)
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question