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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:21:07+00:00 2026-06-15T04:21:07+00:00

This is related to my previous question: Coalesce fields in a .net MVC 4

  • 0

This is related to my previous question: Coalesce fields in a .net MVC 4 model without getting "Only initializers, entity members, and entity navigation properties are supported" from LINQ

After adding a static LINQ expression to my model I am able to use that expression in method-style LINQ statements when selecting from an enumerable of that model class directly (as in the selected answer to the above question, and the example below). Now I must try to use that expression from a related model and am once again exceeding my knowledge of LINQ and EF.

The simplified models involved are:

//The model class for Items for sale in the system
public class Item
{
    [Key, Column(Order = 0)]
    public String CompanyId { get; set; }

    [Key, Column(Order = 1)]
    public String ItemId { get; set; }

    public Int32 CategoryId { get; set; }

    [ForeignKey("CategoryId")]
    public virtual GlobalCategory GlobalCategory { get; set; }

    [ForeignKey("CompanyId, CategoryId")]
    public virtual CompanyCategory CompanyCategory { get; set; }

    //LINQ Expression to coalesce a company specific category alias
    //with the default global category when no alias exists
    public static Expression<Func<Item, String>> linqCategoryName
    {
        get
        {
            return i => (i.CompanyCategory.CategoryAlias == null || 
                         i.CompanyCategory.CategoryAlias == "") ?
                         i.GlobalCategory.CategoryName :
                         i.CompanyCategory.CategoryAlias;
        }
    }
}

and

//The model class for Item Relationships, used to relate two items 
//(a "Parent Item" and "Related Item") such as one is a supply for to the other
public class ItemRelationship
{
    [Key]
    public Int32 RelationshipId { get; set; }

    public Int32 RelationshipTypeId { get; set; }

    [ForeignKey("RelationshipTypeId")]
    public virtual RelationshipType RelationshipType { get; set; }

    public String CompanyId{ get; set; }

    public String ItemId{ get; set; }

    [ForeignKey("CompanyId, ItemId")]
    public virtual Item ParentItem { get; set; }

    public String RelatedCompanyId{ get; set; }

    public String RelatedItemId{ get; set; }

    [ForeignKey("RelatedCompanyId, RelatedItemId")]
    public virtual Item RelatedItem { get; set; }
}

The LINQ Expression member of the Item class can be used successfully with this kind of LINQ statement:

var categories = usscodb.Items.Where(i => i.CompanyId = siteCompanyId)
                               .OrderBy(Item.linqCategoryName)
                               .Select(Item.linqCategoryName)
                               .Distinct();

However, I am unsure how to use the static Item.linqCategoryName in a situation such as this:

var categories = db.ItemRelationships
                .Where(ir => ir.RelationshipType.RelationshipTypeName == "Supply"
                    && ir.ParentItem.Active
                    && ir.RelatedItem.Active
                    && ir.ParentItem.CompanyId == siteCompanyId
                    && ir.RelatedItem.CompanyId == siteCompanyId)

                //the linqCategoryName member is not available for use this way
                .Select(ir.ParentItem.linqCategoryName)
                .Distinct();

I think I’m close here. I’ve tried explicitly joining the Item Relationship to Item using the Parent Item’s keys and then attempting to use the static LINQ expression, but I think since there are two Items present in the Item Relationship LINQ can’t determine which I want. I’m not sure how to explicitly tell LINQ from which Item I want to use the static method.

  • 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-15T04:21:08+00:00Added an answer on June 15, 2026 at 4:21 am

    Try this:

    .Select(ir => ir.ParentItem)
    .Select(Item.linqCategoryName)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is related to: a previous question BUT the question is my code only
This question is directly related to my previous question ASP.NET AJAX Is it possible
This is related to my previous question I'm solving UVA's Edit Step Ladders and
This is related to my previous question More than 1 Left joins in MSAccess
This is related to my previous question here . I want 4 divs (absolute
This is related to my previous question , but a different one. I have
This is related to my previous question , regarding pulling objects from a dmp
This question is related to a previous question of mine That's my current code
This question is related to my previous question How to generate Cartesian Coordinate (x,y)
This question is related to my previous question . The storyline: I have an

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.