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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:54:10+00:00 2026-06-15T07:54:10+00:00

I am new to Linq. I have been using strongly typed data-sets but like

  • 0

I am new to Linq. I have been using strongly typed data-sets but like the idea of Linq to create objects.

At the moment I use datasets to retrieve a table that joins data from tblProducts and tblProductSettings.

This is a one to many relationship as the products serve multiple websites with a unique tblSettings row for each website, however its assumed that there is always one row for tblSettings per website.

Example :

Select * from tblproducts, tblProductSettings where tblproducts.ProdID = tblProductSettings.ProdID and tblProductSettings.CreatorID = @CreatorID

The dataset will give a nice table to loop through and display the products with the settings.

Linq will give me a tblProducts object with tblProductSettings as a collection. I don’t want to have to loop a collection each time to get the settings as this will, i believe create a new sql query for each product and make accesses the settings a pain.

I can create a custom object tblProductsAll, but the only way I can fill this object is…

var Product = from p in context.tblProducts
              join ps in context.tblProductSettings on p.ProdID equals ps.ProdID
              where p.CreatorID == 1 && ps.ProdCreatorDisplay == 1
              select new tblProductsAll() 
              { 
                   ProdTitle = p.ProdTitle,
                   ProdPrice = ps.ProdPrice
              };

I can then pass this model to the razor template.

My question is… is there a better way of setting the properties ? There are 20 or more different properties and i don’t want to have to keep setting them, repeating code, every time i want the same object somehwere in the site with a differnt where clause. Is there a way to stop this repetition ? i.e. automatically mapping the object, or reusing the select new statement.

Any help would be much appreciated.

  • 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-15T07:54:11+00:00Added an answer on June 15, 2026 at 7:54 am

    When there are navigation properties, use them! Your Product entity has a navigation property ProductSettings. That means that the query could look like this:

    var products = from p in context.Products
                   from ps in p.ProductSettings
                   where p.CreatorID == 1 && ps.ProdCreatorDisplay == 1
                   select new ProductsAll() 
                   { 
                        ProdTitle = p.ProdTitle,
                        ProdPrice = ps.FirstOrDefault().ProdPrice
                   };
    

    (Note that I stubbornly refuse to use the tbl prefix)

    But that does not tackle the repetitive code issue.

    Enter AutoMapper. This is a nice little tool that removes boilerplate property copying statement from your code by defining and executing mappings between objects. The default mapping is based on name convention. E.g. the statement Mapper.CreateMap<Product,ProductDto>(); configures a mapping between all equally named properties of Product and ProductDto. And Mapper.Map<ProductDto>(product); would create a ProductDto object from product.

    But your ProductsAll class has a property ProdPrice that comes from ProductSettings. How to deal with that?

    Here a nice feature of AutoMapper can be used. It also resolves properties of nested objects based on name convention. Suppose your class Product had a property Title, AutoMapper can resolve that to a property ProductTitle in the target object. Let’s use that:

    class ProductsAll
    {
        // ProductSetting should have a Product property.
        public string ProductTitle { get; set; }
        // Assuming ProductSetting has a property ProdPrice.
        public decimal ProdPrice { get; set; }
    }
    
    ...
    
    Mapper.CreateMap<ProductSetting,ProductsAll>(); // ProductSetting!
    
    var q = from ps in context.ProductSettings
            where ps.Product.CreatorID == 1 && ps.ProdCreatorDisplay == 1;
    
    // Now the magic!
    var products = q.Project().To<ProductsAll>();
    

    Project() is an extension method on IQueryable. The nice thing is that the SQL statement only contains the properties used in the projection. As you see, you can define a mapping once and for the rest use the Project().To() construct in your code.

    There’s much more to AutoMapper. This is a quick start.

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

Sidebar

Related Questions

I'm new to grails, but have previous experience using .net with c# and linq
I am pretty new to using LINQ and have been trying it out querying
I am new to Linq and previously have been using Ado.net classes. I want
I am new to LINQ to SQL, but have done a lot of database
This should be pretty simple, but I am new at LINQ. I have a
I have been using the System.Data.SQLite provider for my application. When I try to
I've been using LINQ for a while now, but seem to be stuck on
I am new to .NET and LINQ to SQL. So far, I have been
I'm fairly new to programming in general but have been working with Java for
I have been using Entity Framework and now wanted to query with LINQ code.

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.