LINQ and creating NON anonymous return values

I think I understand that I am returning anonymous type records from. But in this I want to create NEW CatalogEntries and set them from selected values. (context is the Devart LinqConnect database context that allows me to get the view).

My solution works, but it seems clunky. I want to do this in one of the instructions.

var query = from it in context.Viewbostons
            select it;
foreach (GPLContext.Viewboston item in query)
    CatalogEntry card = new CatalogEntry();
    card.idx = item.Idx;
    card.product = item.Product;
    card.size = (long)item.SizeBytes; = item.Date.ToString();
    card.type = item.Type;
    card.classification = item.Classification;
    card.distributor = item.Distributor;
    card.egplDate = item.EgplDate.ToString();
    card.classificationVal = (int)item.ClassificationInt;
    card.handling = item.Handling;
    card.creator = item.Creator;
    card.datum = item.Datum;
    card.elevation = (int)item.ElevationFt;
    card.description = item.Description;
    card.dirLocation = item.DoLocation;
    card.bbox = item.Bbox;
    card.uniqID = item.UniqId;
CatalogResults response = new CatalogResults();


I just tried this:

        var query2 = from item in context.Viewbostons
                     select new CatalogResults
                    { item.Idx,


But I am getting the following error:

Error 79 Unable to initialize type 'CatalogService.CatalogResults' using collection initializer because it does not implement 'System.Collections.IEnumerable' C: \ Users \ ysg4206 \ Documents \ Visual Studio 2010 \ Projects \ CatalogService \ CatalogService \ CatalogService.svc. cs 91 25 CatalogService

I have to tell you what the definition of CatalogResults is, what I want to return:

public class CatalogResults
    CatalogEntry[] _results;

    public CatalogEntry[] results
        get { return _results; }
        set { _results = value; }


Today my mind is boring, apologizes to everyone. You help. The end result will be serialized by WCF to a JSON structure, I need an array wrapped in an object with some size information, etc.


source to share

4 answers

Since .NET 3.0

you can use an object initializer like below:

var catalogResults = new CatalogResults 
      results = context.Viewbostons
                       .Select(it => new CatalogEntry 
                            idx = it.Idx,
                            product = it.Product,


So, if this is only one place where you use the property setters CatalogEntry

- all properties are read-only, so CatalogEntry

will be immutable.

MSDN, Object Initializer :

Object initializers allow you to assign values ​​to any available fields or properties on an object at creation time without the need for an explicit constructor call.



This is where you create an IQueryable and then accept FirstOrDefault () as the response (if you want a single response) or ToArray () (if you want an array). The error you are getting (Error 79 Cannot initialize type "CatalogService.CatalogResults" with collection initializer because it does not implement "System.Collections.IEnumerable") because you are trying to create an IEnumerable on a CatalogEntry object (by referencing the item variable).

    var response = (from item in context.Viewbostons
                 select new CatalogEntry() 
                     idx = item.Idx,
                     product = item.Product,
                     size = (long)item.SizeBytes,




You don't need to create anonymous types in the Linq element. You can specify your real type.

var query = context.Viewbostons.Select( it => 
    new CatalogEntry
       idx = it.idx,
       ... etc




This should work:

var query = from it in context.Viewbostons
            select new CatalogEntry()
                // ...




All Articles