Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6388113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:14:18+00:00 2026-05-25T03:14:18+00:00

I’m using EF 4.1 and I’m trying to enumerate a company list for a

  • 0

I’m using EF 4.1 and I’m trying to enumerate a company list for a grid. I have two options in the current project: select all companies from the DbContext (Entities) and load them into an object from a non-anonymous type (let’s say EmpresaGrid) or select all companies into anonymous type objects with the same structure like Empresa (which is the entity I’m selecting from).

The first option (creating a model class for that) would require a little more work, but can be, eventually, more readable. Still, I’m not sure about that. The second option is what I’m using right now.

So, first question: it’s better to create a model class only for displaying data or use anonymous type? Doing a direct select is out of question: a SELECT * is too big and that might make everything damn slow (I guess). So selection into another type creates a custom query with only the needed fields.

Using the second option (anonymous type), I have this code (simplified version):

public static IEnumerable<object> Grid()
{
    Entities db = new Entities();

    var empresas = db.Empresas
        .Select(e => new
        {
            Cgc = e.Cgc, // PK
            (...)
            Address = new
            {
                AddressLine = e.EnderecoSede.AddressLine,
                (...)
            }
        },
        Contato = e.Contato,
        (...)
    })
    .ToList();

    return empresas;
}

The anonymous type I’m creating has around 40 lines of code, so it’s kinda big, but it recreates part of the Empresa class struct (since the grid is waiting for a Empresa object). Anyway, I have a problem with the data format. For example, I would like to format the Cgc property using a custom string format. I have a public method for this, FormataCgc. This method receives a string and returns it formatted using some internal conditions.

So, my problem is how to that. For example, I have tried this:

var empresas = db.Empresas
    .Select(e => new
    {
        Cgc = FormataCgc(e.Cgc),
    }

But that doesn’t work because FormataCgc cannot be translated into SQL (and I don’t want to convert it). I also tried this:

var empresas = db.Empresas
    .Select(e => new
    {
        (...)
    }
    .ToList();

foreach (var e in empresas) {
    e.Cgc = FormataCgc(e.Cgc);
}

But it cannot be done since anonymous types have only read-only properties.

So, my second question is: how exactly can I do that? I need to change the data after selecting it, but using anonymous types? I’ve done a little research, and the best thing I’ve found was this: Calling a custom method in LINQ query. In that solution, Ladislav suggested doing a second select from the IEnumerable, but since the grid is excepting Empresa I cannot do that (I need to change or add properties, not encapsulate them).

I’m not sure if I was clear enough, but feel free to ask any questions. Also, the grid I’m currently using is a Telerik ASP.NET MVC Grid, which receives a IEnumerable (where T is a class) as model data and them iterates each object, doing its magic.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T03:14:18+00:00Added an answer on May 25, 2026 at 3:14 am

    Since you’re already converting this into an IEnumerable<T>, you can do the custom formatting as you stream the results in the client. Do your db.Select, and then convert to the appropriate format afterwards, ie:

    var empresas = db.Empresas
        .Select(e => new
        {
            (...)
        })
        .ToList();
    
    foreach (var e in empresas) {
        yield return new {
           Cgc = FormataCgc(e.Cgc),
           // Copy other properties here, as needed...
        };
    }
    

    That being said, I’d personally recommend making a custom class, and not return an anonymous type. Your conversion would then be:

    foreach (var e in empresas) {
        yield return new YourClass(FormataCgc(e.Cgc), ...); // Construct as needed
    }
    

    This will dramatically improve the usability of this method, as you will have proper, named access to your properties from the caller of the method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.