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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:18:51+00:00 2026-06-11T17:18:51+00:00

I have a Person object: public class Person { public string Name { get;

  • 0

I have a Person object:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Address { get; set; }
    public double Height { get; set; }
    public bool IsTest { get; set; }
}

Then I have a list filled with different Person objects.

I want to know is there a way to use ternary operator with GroupBy in LINQ depending on some property of object. For example:

var groupedPersons = persons.GroupBy(person => person.IsTest ? 
                      new {
                              person.Name, 
                              person.Age, 
                              person.Address
                           } 
                     : new {
                              person.Name, 
                              person.Age, 
                              person.Address, 
                              person.Height}).ToList();

But unfortunately that doesn’t work, gives me exception

Type of conditional expression cannot be determined because there is
no implicit conversion between ‘AnonymousType#1’ and ‘AnonymousType#2’

Is this achievable at all and how?

Thanks

EDIT: Tried this, but not working.

var groupedPersons = persons.GroupBy(person => person.OnTest ? 
                     new Person { 
                                   Address = person.Address, 
                                   Name = person.Name, 
                                   Age = person.Age } 
                     : new Person { 
                                   Address= person.Address, 
                                   Name = person.Name, 
                                   Age = person.Age , 
                                   Height = person.Height}).ToList();

EDIT: Got it to work, look at my answer

  • 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-11T17:18:52+00:00Added an answer on June 11, 2026 at 5:18 pm

    Ok, after trying some examples shown here I realized that this is not gonna work and I got it to work other way. Here’s an example how I did it:

    var groupedPersons = 
           persons.GroupBy(
                     person =>
                          person.Name + "|" +
                          person.Age.ToString() + "|" +
                          person.Address + "|" +
                          (person.IsTest ? null : person.Height.ToString())
          .ToList();
    

    So if there’s a Person with property IsTest = false then it uses person.Height to group also.

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

Sidebar

Related Questions

Suppose i have class Person { public int Id {get;set;} public string Name {get;set;}
Lets say I have class Person { public Person(int age, string name) { Age
I have the following Model: public class Person { public string Name {get;set;} public
Lets say I have an object class Person { public string Name { get;
i have class like below class Person { public string Name{get;set;} public string Surname{get;set;}
I have a class Like public class Person { public string Name{ get; set;
Given the classes: public class Person { public string Name { get; set; }
Say i have the following: public class Person{ public int ID {get; set;} public
I've got simple class public class Person { string Name { get; set; }
I have an object that has a child collection: class Person { public string

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.