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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:51:30+00:00 2026-05-16T01:51:30+00:00

I have a class say public class CostStore { int DirectorateId { get; set;

  • 0

I have a class say

public class CostStore
{
    int DirectorateId { get; set; }

    decimal NewCar { get; set; }

    decimal ContractorNew { get; set; }

    decimal ContractorRenew { get; set; }

    decimal ContractorLost { get; set; }

    decimal ContractorStolen { get; set; }

    decimal InternalNew { get; set; }

    decimal InternalRenew { get; set; }

    decimal InternalLost { get; set; }

    decimal InternalStolen { get; set; }        
}

and in my controller i want to find out say

var c = from p in _service.List().Where (condition) p.InternalNew/InternalRenew

(etc) property based on a session variable like so in the function below, how can I do this in linq statement…any ideas (_service.List() lists the IEnumerable of the class CostStore

private string FindProperty()
{
    switch (Session[Constants.FORMSESSIONKEY].ToString())
    {
        case Constants.NEWAPP:
            return "InternalNew";
        case Constants.LOST:
            return "InternalLost";
        case Constants.NEWCAR:
            return "NewCar";
        case Constants.OTHER:
            return "InternalStolen";
        case Constants.RENEW:
            return "InternalRenew";
        default:
            return String.Empty;  
     }
}

Currently I am having to do this

private Decimal FindProperty()
{
    switch (Session[Constants.FORMSESSIONKEY].ToString())
    {
        case Constants.NEWAPP:
            return (from p in _costStoreService.List().Where(p => p.DirectorateId == _applicant.DirectorateId)
                    select p.InternalNew).Single() ?? 0.0M;
        case Constants.LOST:
            return (from p in _costStoreService.List().Where(p => p.DirectorateId == _applicant.DirectorateId)
                    select p.InternalLost).Single();
        case Constants.NEWCAR:
            return (from p in _costStoreService.List().Where(p => p.DirectorateId == _applicant.DirectorateId)
                    select p.NewCar).Single();
        case Constants.OTHER:
            return (from p in _costStoreService.List().Where(p => p.DirectorateId == _applicant.DirectorateId)
                    select p.InternalStolen).Single();
        case Constants.RENEW:
            return (from p in _costStoreService.List().Where(p => p.DirectorateId == _applicant.DirectorateId)
                    select p.InternalRenew).Single();
        default:
            return 0.0M;
        }
    }

but its a lot of duplicate code, also no check for sequence containing 0 values…

  • 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-16T01:51:30+00:00Added an answer on May 16, 2026 at 1:51 am

    I’m assuming your service returns IQueryable<T>. You can use an Expression to define projection outside of your query:

        private Expression<Func<CostStore,Decimal>> GetProjection()
        {
            switch (Session[Constants.FORMSESSIONKEY].ToString())
            {
                case Constants.NEWAPP: 
                    return c => c.InternalNew;
                case Constants.LOST:
                    return c=> c.InternalLost;
                // ... etc, you get the idea
                default:
                    return c => 0m; // or some other sensible default
                   // break;
            }
        }
    

    If your service returns IEnumerable<T>, use Func<CostStore,decimal> instead.

    You can use it like this:

    var projection = GetProjection();
    var c = _service.List().Where (condition).Select(projection) 
    

    EDIT: put this in a console application, study and learn.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace ConsoleApplication13
    {
    class Program
    {
        static void Main(string[] args)
        {
            var _service = new Service();
    
            Func<CostStore, bool> condition = s => s.DirectorateId == 10;
            Func<CostStore, decimal> projection = GetProjection();
    
            var c = _service.List().Where(condition).Select(projection);
        }
    
        private static Func<CostStore, decimal> GetProjection()
        {
            return c => c.NewCar;
        }
    
        class Service
        {
            public IEnumerable<CostStore> List()
            {
                return new List<CostStore>();
            }
        }
    
        public class CostStore
        {
            public int DirectorateId { get; set; }
            public decimal NewCar { get; set; }
        }
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have this class: class myclass { public int Field1{ get; set; }
Say I have: public class Event { public int EventID { get; set; }
Say I have this class: public class Account { public int AccountID { get;
I have a class lets say students with properties as public string sname{get;set;} public
I have an employee object say: public class Employee { public int Id {get;
say I have: class Test { public static int Hello = 5; } This
Say I have a class: public class R { public static final int _1st
Suppose i have two methods in a class say public void eat(int i,String s)
Let's say I have: class A{ public: int x; int y; }; And I
I have class say for example public class Item { int price; String name;

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.