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 6917221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:42:10+00:00 2026-05-27T09:42:10+00:00

When I try to Cast the projected list in the BuildTypes method, I get

  • 0

When I try to Cast the projected list in the BuildTypes method, I get a list of null values. I have also tried using the .Cast(), but I get an error that some of the properties cannot be cast. I can post the error if it is helpful. Here is my code:

public class AuditActionType: EntityValueType
{
}

private List<T> BuildTypes<T>(XDocument xDocument) where T: EntityValueType
{
    var types = 
        (from ty in xDocument.Descendants("RECORD")
         select new
            {
                Id = GenerateGuid(),
                Name = ty.Element("Name").Value,
                EntityStatus = _activeEntityStatus,
                DateCreated = DateTime.Now,
                DateModified = DateTime.Now
            } as T).ToList();

    return types;
} 

So I would call it like this:

var auditActorTypes = BuildTypes<AuditActorType>(auditActorTypesXml)

I have a ton of types I need to pull from an XML file and didn’t want to duplicate code for each type.

  • 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-27T09:42:11+00:00Added an answer on May 27, 2026 at 9:42 am

    You are trying to cast an anonymous object as type T, which cannot be done. The anonymous type is it’s own unique type and in no way related to the T passed in.

    Instead, you could supply the new() constraint on type T to mean it needs a default constructor, and then perform a new T() instead of creating a new anonymous type:

    private List<T> BuildTypes<T>(XDocument xDocument) where T: EntityValueType, new()
    {
        var types = 
            (from ty in xDocument.Descendants("RECORD")
             select new T()
                {
                    Id = GenerateGuid(),
                    Name = ty.Element("Name").Value,
                    EntityStatus = _activeEntityStatus,
                    DateCreated = DateTime.Now,
                    DateModified = DateTime.Now
                }).ToList();
    
        return types;
    } 
    

    This is assuming, of course, that Id, Name, EntityStatus, DateCreated, and DateModified are all properties of the base EntityValueType.

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

Sidebar

Related Questions

I have en error when try to cast own enumerator to address book values:
I try to cast an object to my Action class, but it results in
How do I get an NSString from console input, and then try to cast
Try loading this normal .jpg file in Internet Explorer 6.0. I get an error
I have the following scenario, with exception being thrown when I try to cast:
I have downloaded DeepEmailMiner-v1-source.zip from http://sourceforge.net/projects/deepemailminer/files/DeepEmailMiner/Version%20v1/ . But when I try to execute DeepMailMiner.java
I have LinkButton and HiddenField in a list view. I want to get the
I'm using SQL Server 2008 and my fields have DATE type but in Visual
Try to see which cast is faster (not necessary better): new c++ case or
When I try to use a static_cast to cast a double* to an int*,

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.