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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:53:43+00:00 2026-06-15T01:53:43+00:00

Can the following be done with a single LINQ expression? I have a list

  • 0

Can the following be done with a single LINQ expression? I have a list of Item:

public enum ItemType
{
    Top,
    Bottom,
    Side,
    CornerBottom,
    CornerTop
}

public class Item
{
    public int ID;
    public ItemType Type;
}

I want to select an Item based on the ItemType – however, there is a special case with CornerBottom and CornerTop. If I’m searching for a CornerBottom Item, and there are no such results, I would instead like a Bottom Item. If there are results for that, then I want a Side Item. Likewise with CornerTop, I prefer a CornerTop Item first, then Top second, then Side third.

Can this be done with a single expression? Right now I have a switch statement handling each ItemType. However, this is unwieldy, because in reality my Item class is more complicated (and I have more ItemTypes), and I have a lot of different queries that need this core functionality.

EDIT:
Okay, my final solution is this. I reordered the ItemType enum with my preferred search order, as recommended by Spencer. Then I just put the custom comparison code in another function (which will be easy to reuse), and just orderby and return the first value.

ItemType searchType = ItemType.CornerTop;

var i = (from item in Items
        where typeCheck(item, searchType)
        orderby item.Type descending).FirstOrDefault();

bool typeCheck(Item item, ItemType type)
{
    switch (type)
    {
        case ItemType.CornerTop:
            return (item.Type == ItemType.CornerTop)
                || (item.Type == ItemType.Top)
                || (item.Type == ItemType.Side);
        case ItemType.CornerBottom:
            return (item.Type == ItemType.CornerBottom)
                || (item.Type == ItemType.Bottom)
                || (item.Type == ItemType.Side);
        default:
           return item.Type == type;
    }
}
  • 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-15T01:53:45+00:00Added an answer on June 15, 2026 at 1:53 am

    Give each enum value a numeric value in the order of precedence. Sort your list of items by the Type and grab the first.

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

Sidebar

Related Questions

How can I self-sign an iPhone application using Xcode? I have done the following:
If use MongoRepository, You can have following code: @Repository public interface UserRepo extends MongoRepository<User,
Can the following be done in a single line? typedef BOOST_TYPEOF(generator) G; typename G::value_type
This can be done in a million ways ... Problem. I have a list
Essentially I'm wondering if the following can be done in Ruby. So for example:
I have several files which is causing a compile error, can the following be
I have the following two classes. public class Foo { public Bar Args {
I have written a list-like template class sll (Single Linked List). Now, I am
I have to get values from HTML document, I can't use full xpath expression
I have the following XML structure, which is modelling a single concept across multiple

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.