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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:45:35+00:00 2026-06-18T20:45:35+00:00

//In a Static class ,extension implementation public static IQueryable<T2> ToDTO<T, T2>(this IQueryable<T> source) {

  • 0
 //In a Static class ,extension implementation
 public static IQueryable<T2> ToDTO<T, T2>(this IQueryable<T> source)
    {
        return source.To<T2>();
    }

//Usage
var result = personType1Queryable.ToDTO< personType1, personType2>();

In above code as you see its an extension. It converts one type to another. So first this referenced object is personType1Queryable typed IQueryable < personType1 > I just want to call this function like this;

personType1Queryable.ToDTO<personType2>();

I just want to pass just destination type. Because this referenced object already passed. But compiler doesn’t accept this why? For Where < T > extension in Linq it works.
For me why not?

EDIT: I applied Eren’s answer. But it seems still something missing.

  //works
    public static IQueryable<T2> ToDTO<T,T2>(this IQueryable<T> source)
    {
        return source.Project().To<T2>();
    }

  //gives object reference error. Shown below!
    public static IQueryable<T> ToDTO<T>(this IQueryable<object> source)
    {
        return source.Project().To<T>();
    }

enter image description here

https://i.stack.imgur.com/9Yh6H.png

  • 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-06-18T20:45:36+00:00Added an answer on June 18, 2026 at 8:45 pm

    IQueryable<T> is covariant in T, so depending on what you need in the To<T> extension, you might get away with this:

    public static IQueryable<T> ToDTO<T>(this IQueryable<object> source)
    {
        return source.To<T>();
    }
    

    Note that this will only work if the type parameter of the original IQueryable<T> (e.g. personType1) is a reference type. Otherwise, you’ll get a compiler error.

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

Sidebar

Related Questions

The definition of my extension method is this: public static class HtmlHelperExtensions { public
I am using Ladislav Mrnka's extension method: public static IQueryable<T> IncludeMultiple<T>(this IQueryable<T> query, params
I'm trying to code a Linq MinBy extension method public static class Extensions {
i wrote extension class, for textbox control. public static class Extensions { public static
I have an extension method with the following signature (in BuildServerExtensions class):: public static
Is there any way I can add a static extension method to a class.
I have the following property definition: public static class Extensions { public static readonly
public static class TestTask extends AsyncTask<Void, Integer, Integer> { private String stacktrace; public TestTask
public static class MyClass { // why it is allowed ? public static Random
The class looks as follows: public static class CacheManager { private static Dictionary<string, object>

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.