-3

I have a problem with converting linq result to object.

I have a class called Plant and a database which contains information about it (for example name, latin name, habitats etc). I want to create a new object from executed query and send it to another part of application. So I'm messing with this code:

using (DataClassesDataContext dc = new DataClassesDataContext())
{

    var sPlant = (from p in dc.Plants where p.Name == plantName select new Plant
    {
        Name = p.Name,
        LatinName = p.LatinName, 
        Habitat = p.Habitat,
        LeafHarvesting = p.LeafHarvesting,
        FlowerHarvesting = p.FlowerHarvesting,
        FruitHarvesting = p.FruitHarvesting,
        RootHarvesting = p.RootHarvesting,
        Morphology = p.Morphology,
        Pharmacology = p.Pharmacology,
        Img = p.Img,
        GPSCoordinates = p.GPSCoordinates
    } 
);

But it doesn't convert result to a new Plant object.

xlecoustillier
  • 16,183
  • 14
  • 60
  • 85
vortex
  • 107
  • 12

3 Answers3

1

Use First or FirstOrDefault function to get the object. See here to get the difference.

Community
  • 1
  • 1
aguetat
  • 514
  • 4
  • 18
1

As it seems that Plant is not part of the data store, you need to return an object that Linq to SQL can handle, to then create your Plant instance locally.

Start by querying for a list of anonymous objects containing the properties you need, and then only create your Plant. Add a First() or a FirstOrDefault() at the end to retrieve only one Plant:

using (DataClassesDataContext dc = new DataClassesDataContext())
{
    var sPlant = (from p in dc.Plants where p.Name == plantName
        select new {
            Name = p.Name,
            LatinName = p.LatinName, 
            Habitat = p.Habitat,
            LeafHarvesting = p.LeafHarvesting,
            FlowerHarvesting = p.FlowerHarvesting,
            FruitHarvesting = p.FruitHarvesting,
            RootHarvesting = p.RootHarvesting,
            Morphology = p.Morphology,
            Pharmacology = p.Pharmacology,
            Img = p.Img,
            GPSCoordinates = p.GPSCoordinates
        }).AsEnumerable().Select(p => new Plant
        {
            Name = p.Name,
            LatinName = p.LatinName, 
            Habitat = p.Habitat,
            LeafHarvesting = p.LeafHarvesting,
            FlowerHarvesting = p.FlowerHarvesting,
            FruitHarvesting = p.FruitHarvesting,
            RootHarvesting = p.RootHarvesting,
            Morphology = p.Morphology,
            Pharmacology = p.Pharmacology,
            Img = p.Img,
            GPSCoordinates = p.GPSCoordinates
        }).First();
}
xlecoustillier
  • 16,183
  • 14
  • 60
  • 85
  • An unhandled exception of type 'System.NotSupportedException' occurred in System.Data.Linq.dll – vortex Oct 12 '15 at 12:28
  • explicit construction unit type "Zielnik.Plant" in the query is not allowed. – vortex Oct 12 '15 at 12:29
  • it results in the same exception – vortex Oct 12 '15 at 12:37
  • edited again... I'm a bit rusty, i'll have to find a way to test it. – xlecoustillier Oct 12 '15 at 12:39
  • it ended with "SQL Server doesn't provide comparing data types NText, Text, Xml ani Image. – vortex Oct 12 '15 at 12:45
  • See [this question](http://stackoverflow.com/q/1568976/1679537) or [this post](http://forums.asp.net/t/1813435.aspx?+SQL+Server+does+not+handle+comparison+of+NText+Text+Xml+or+Image+data+types+). I guess the problem comes from your `Img` field. – xlecoustillier Oct 12 '15 at 12:47
  • I changed all datatypes in database to nvarchar(max) Code is getting the error "explicit construction type Zielnik.plant in the query is not allowed – vortex Oct 12 '15 at 12:56
0

If your query is suppose to return multiple results, use .TOList(). If you want to take first row only,use FirstOrDefault()

Shetty
  • 1,792
  • 4
  • 22
  • 38