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

  • Home
  • SEARCH
  • 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 3274778
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:04:31+00:00 2026-05-17T19:04:31+00:00

Using Visual Studio 2010 .Net Framework 4 C# Linq to Entities Issue I would

  • 0

Using

  • Visual Studio 2010
  • .Net Framework 4
  • C#
  • Linq to Entities

Issue

I would like to be able to apply Object Oriented Principles like DRY and SOLID to some Linq Projections. With compiled queries or passed parameters I can apply these to the rest of Linq successfully so far, just not in the projections.

Please let me know if this isn’t possible, and I must choose one of my alternate solutions (described below), if it is possible then how, or if I am missing something and there is another alternative implementation that will satisfy the goal.

Details

At a high level I would like to be able to dynamically control the type used in a Linq Projection, either with a standard Linq Query or a CompiledQuery. I am using Linq to Entities in my examples and actual code, however the issue should be applicable to the core Linq.

Below are simplistic examples that are not dynamic and do not solve the issue. They are fixed to always use the FooUser for each type. What I would like to be able to do is dynamically control the type of user created in the projection all of which would be based on a common IUser interface. This would be or could be similar to how I can control what type the query filters on.

Alternate Solutions

I am trying to conform to DRY, SOLID, and also trying to avoid using an enum to deal which is a typical code smell. However in all my attempts and research I seem to have to fall to one of the following solutions.

  1. Implement a query for each type
    which are all the same except for
    the type they filter on and the type
    used int he projection. While this
    violates DRY and OCP, I can
    encapsulate this within a single
    class and keep them close together
    as complied queries. This will
    require the class to change if I add
    a new type or if how you query for
    the data changes.

  2. Implement a enum that has the types, and use a more generalized User class that has its type as a property. However this will cause me to have to use the enum in several locations and introduce long case statements to handle them, which I would like to avoid.

I would love not to have to choose between different evils, and have an implementation that can conform to all SOLID principles and DRY. However if I must I think I will end up with the first or a version of it.

Examples

Standard Simple Linq Query

using (MyEntities context = new MyEntities())
{
    var results = from u in context.Users
                  where u.UserType == type
                  select new FooUser
                  {
                      Id = u.UserID,
                      Name = u.UserName,
                      Location = u.UserLocation
                  };
}

Compiled Version of the Above Query

private static readonly Func<MyEntities, int, IQueryable<FooUser>> query = CompiledQuery.Compile<MyEntities, int, IQueryable<FooUser>>(
    (context, type) => from u in context.Users
                       where u.UserType == type
                       select new FooUser
                       {
                           Id = u.UserID,
                           Name = u.UserName,
                           Location = u.UserLocation
                       });-
  • 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-17T19:04:31+00:00Added an answer on May 17, 2026 at 7:04 pm

    I found a way to do this with a standard method call. I haven’t figured out how to do it with a compiled query, it doesn’t look likely.

    I was not aware of the Constructor Constraint I on the where statement for a generic. This can suit my needs. I would love to do this with a compiled query, but can live happy with this solution.

    public IQueryable<IUser> FooMethod<T>(int type) where T : IUser, new()
    {
        using (MyEntities context = new MyEntities())
        {
            var results = from u in context.users
                          where u.usertype == type
                          select new T
                          {
                              id = u.UserId,
                              name = u.UserName,
                              location = u.Userlocation
                          };
            return results; 
        }
    }
    

    I chose to post an answer instead of deleting the question for two reasons, one in case others are looking for something similar it could be helpful. Then of course I could be way off base and its is always fun to have people shoot holes in things and see what better stuff we can come up with.

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

Sidebar

Related Questions

I'm using Entity Framework in Visual Studio 2010 Beta 2 (.NET framework 4.0 Beta
I'm using Visual Studio 2010 RTM with .NET/Entity Framework 4 RTM with a model
I'm trying to debug .NET Framework's source code using Visual Studio 2010 Professional. I
Background Information: Visual Studio 2010 (.NET Framework 4.0) Informix Server 7.31 Informix ClientSDK 3.50
Can I develop asp.net 3.5/2.0 projects using Visual Studio 2010? Or I will be
When using Visual Studio (though ideally this can apply to the generic case) and
Using Visual Studio 2010, when I add the four IronRuby references to an existing
I have many Visual Studio 2008 web projects targeted at the .NET Framework 3.5.
I have created an OData/WCF service using Visual Studio 2010 on Windows XP SP3
Using Visual Studio 2008 Team Edition, is it possible to assign a shortcut key

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.