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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:46:04+00:00 2026-05-27T01:46:04+00:00

I have created a Extension method for IQueryable class to convert list of a

  • 0

I have created a Extension method for IQueryable class to convert list of a Generic source class to list of other generic destination class,(.net3.5)
I’m using reflection to get properties from source object value and assign it to destinvation object.

The source code of the class is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.RegularExpressions;

namespace System.Linq
{
    public static class QueryableExtensions
    {
        public static IQueryable<TDest> ToDTO<TDest>(this IQueryable<TSource> source)//getting error on this line
        {
            return DTOTranslator<TDest>.ConvertToDTO<TDest>(source);
        }
    }

    public class DTOTranslator<TSource>
    {
        public static IQueryable<TDest> ConvertToDTO<TDest>(IQueryable<TSource> source)
        {
            List<TDest> destinationList = new List<TDest>();
            List<TSource> sourceList = source.ToList<TSource>();

            var sourceType = typeof(TSource);
            var destType = typeof(TDest);
            foreach (TSource sourceElement in sourceList)
            {
                TDest destElement = Activator.CreateInstance<TDest>();
                //Get all properties from the object 
                PropertyInfo[] sourceProperties = typeof(TSource).GetProperties();
                foreach (PropertyInfo sourceProperty in sourceProperties)
                {
                    //and assign value to each propery according to property name.
                    PropertyInfo destProperty = destType.GetProperty(sourceProperty.Name);
                    destProperty.SetValue(destElement, sourceProperty.GetValue(sourceElement, null), null);
                    destinationList.Add(destElement);
                }
            }

            return destinationList.AsQueryable();
        }
    }
}

however at compile time I am getting the error on line 12:

The type or namespace name 'TSource' could not be found (are you missing a using directive or an assembly reference?)

UPDATE:

Thanks to all for reply.

Now I’ve updated my class as follows:

public static class QueryableExtensions
{
    public static IQueryable<TDest> ToDTO<TSource, TDest>(this IQueryable<TSource> source)
    {
        List<TDest> destinationList = new List<TDest>();
        List<TSource> sourceList = source.ToList<TSource>();

        var sourceType = typeof(TSource);
        var destType = typeof(TDest);
        foreach (TSource sourceElement in sourceList)
        {
            TDest destElement = Activator.CreateInstance<TDest>();
            //Get all properties from the object 
            PropertyInfo[] sourceProperties = typeof(TSource).GetProperties();
            foreach (PropertyInfo sourceProperty in sourceProperties)
            {
                //and assign value to each propery according to property name.
                PropertyInfo destProperty = destType.GetProperty(sourceProperty.Name);
                destProperty.SetValue(destElement, sourceProperty.GetValue(sourceElement, null), null);
                destinationList.Add(destElement);
            }
        }

        return destinationList.AsQueryable();
    }
}

This works fine,

now the only thing I wonder is that-

Is it possible to skip the <TSource> paramerter,

how to read it from the IQueryable’s element type and define its object?
I mean currently I call the method as

IQueryable<CATEGORY_DTO> dtos = (from p in new MyEntities().CATEGORY select p).ToDTO<CATEGORY, CATEGORY_DTO>();

I want to call it as

IQueryable<CATEGORY_DTO> dtos = (from p in new MyEntities().CATEGORY select p).ToDTO<CATEGORY_DTO>();

Thanks, NJ

  • 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-27T01:46:05+00:00Added an answer on May 27, 2026 at 1:46 am

    Try this:

    public static class QueryableExtensions
    {
        public static IQueryable<TDest> ToDTO<TDest, TSource>(this IQueryable<TSource> source)
        {
            return DTOTranslator<TSource>.ConvertToDTO<TDest>(source);
        }
    }
    

    The addition being that the ToDTO method also references the TSource generic type (since otherwise how can you reference is as a parameter – this led to the original error), and this generic type is given in the constructor for DTOTranslator (since it is an error to use TDest in the constructor, as the definition of the generic type needed in the constructor itself is TSource, which is a different type from TDest).

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

Sidebar

Related Questions

I have created a Generic Extension method for DataRow object. The method takes no
I am having an issue using BinaryFormatter.Serialize. I have this generic extension method to
I have created a subclass of a generic list so that I could implement
I have created an HtmlHelper Extension method which returns an encoded string, I have
I created the following extension method for a ViewPage: using System.Web.Mvc; namespace G3Site {
I have an Extension method which is supposed to filter a Queryable object (IQueryable)
i have installed SharePoint 2007 and VS2008 extension on my computer.i have created a
I have created a basic site using ASP.NET routing according to Mike Ormond's example
i have created a simple public ref class in the vc++ project, which is
I have created one table using the below command: create table Table1( Id 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.