0

これは私の前の質問に関連しています:LINQから「初期化子、エンティティメンバー、およびエンティティナビゲーションプロパティのみがサポートされている」を取得せずに.netMVC4モデルのフィールドを合体させる

モデルに静的LINQ式を追加した後、そのモデルクラスの列挙型から直接選択するときに、メソッドスタイルのLINQステートメントでその式を使用できます(上記の質問に対する選択された回答と以下の例のように)。今、私は関連するモデルからその式を使用しようとしなければならず、LINQとEFの知識をもう一度超えています。

関連する簡略化されたモデルは次のとおりです。

//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;
        }
    }
}

//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; }
}

ItemクラスのLINQ式メンバーは、次の種類のLINQステートメントで正常に使用できます。

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

ただし、次のような状況で静的Item.linqCategoryNameを使用する方法がわかりません。

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();

私はここに近いと思います。親アイテムのキーを使用してアイテムリレーションシップをアイテムに明示的に結合してから、静的LINQ式を使用しようとしましたが、アイテムリレーションシップに2つのアイテムが存在するため、LINQはどちらが必要かを判断できないと思います。静的メソッドを使用するアイテムをLINQに明示的に指示する方法がわかりません。

4

1 に答える 1

1

これを試して:

.Select(ir => ir.ParentItem)
.Select(Item.linqCategoryName)
于 2012-11-30T17:38:03.233 に答える