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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:09:48+00:00 2026-05-22T16:09:48+00:00

I am having a go at creating a generic xmlRepository where I can pass

  • 0

I am having a go at creating a generic xmlRepository where I can pass in any type and get a list back.

At the moment my code looks like this (Only way I can get it to work):

public IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class
{
    var filename = GetEntityFileName<TEntity>();
    var doc = XDocument.Load(filename);
    var query = (from p in doc.Descendants(entityName)
                 select (ServiceAccount)p).AsQueryable().Cast<TEntity>();
    return query;
}

I would like to swap out the

select (ServiceAccount)p).AsQueryable().Cast<TEntity>();

with

select (TEntity)p).AsQueryable();

TEntity is the same object as ServiceAccount,

Is this possible with LINQ to XML?

I have a SQLRepository, using EF, which has the same method in it and I am looking for the LINQ to XML equivalent of

((IObjectContextAdapter)_context).ObjectContext.CreateQuery<TEntity>(entityName);

If indeed there is one.

  • 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-22T16:09:49+00:00Added an answer on May 22, 2026 at 4:09 pm

    You need to specify how you’re going to convert an XElement to an instance of T. Currently your code simply won’t work unless ServiceAccount already has an explicit conversion from XElement.

    You may want to have a convention of having a static FromXElement method in each entity type – you could then invoke that from reflection, or even have a dictionary:

    private static readonly Dictionary<Type, Delegate> Converters =
        new Dictionary<Type, Delegate> {
            { typeof(ServiceAccount),
              (Func<XElement, ServiceAccount>) ServiceAccount.FromXElement },
            { typeof(OtherEntity),
              (Func<XElement, OtherEntity>) OtherEntity.FromXElement },
        };
    

    You’d then write something like:

    public IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class
    {
        Delegate converter;
        if (!Converters.TryGetValue(typeof(TEntity), out converter))
        {
            throw new InvalidOperationException("...");
        }
        Func<XElement, TEntity> realConverter = (Func<XElement, TEntity>) converter;
    
        var filename = GetEntityFileName<TEntity>();
        var doc = XDocument.Load(filename);
        return doc.Descendants(entityName)
                  .Select(realConverter)
                  .AsQueryable();
    }
    

    It’s not really clear why you’re using IQueryable<T> here, by the way… anything wrong with just IEnumerable<T>?

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

Sidebar

Related Questions

I'm having trouble creating a sub that can create objects of a variable type
I'm having issues creating an ActionLink using Preview 5. All the docs I can
I am having some trouble creating a nested association and I can't quite spot
i am already creating a table by having a list of json objects. Is
Generic containers can be a time saver when having a item, and a strongly
I am creating a generic class to hold widgets and I am having trouble
I'm having a problem with a generic class I'm creating in Scala. I have
I'm having trouble creating a mouseover function with an NSTableView. The idea is that
I am having trouble creating an XML document that contains a default namespace and
I'm currently having trouble creating an image from a binary string of data in

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.