Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only initializers, entity members, and entity navigation properties are supported. (ASP.NET MVC and Entity Framework)

I am stuck in somewhere on my ASP.NET MVC 3 app. here is the error I am getting :

The specified type member 'AccommPropertyTags' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

I have found how we can solve this on following article :

Only initializers, entity members, and entity navigation properties are supported

But mine is a little bit odd.

Here is one of the partial class of my entity :

[MetadataType(typeof(AccommPropertyWebDetail.MetaData))]
public partial class AccommPropertyWebDetail {

    public virtual ICollection<string> AccommPropertyTags {

        get {

            return Helpers.AccommPropertyTag.CreateStringListFromString(this.PropertyTags);
        }
    }

    private class MetaData {

        [Required, StringLength(50)]
        public string Category { get; set; }

    }
}

As you can see above, AccommPropertyTags property is typeof ICollection<string>. What I am trying inside my controller is as follows :

public ViewResult Tag(string tag) {

    var _rawTag = HttpUtility.UrlDecode(tag);

    ViewBag.Tag = _rawTag;

    var model = _accommpropertyrepo.GetAllAccommPropertiesFullWebIgnoringApprovalStatus();

    model = model.Where(
            x => x.AccommPropertyTags.Any(
                    y => y == _rawTag
                )
        );

    return View(model);
}

Because of the fact that I am using Any there, Entity is trying to convert my AccommPropertyTags property to SQL and cant because it's not part of the table schema.

Am I really stuck here or is there a cool way of beating this annoying error?

like image 933
tugberk Avatar asked Oct 08 '11 13:10

tugberk


1 Answers

Your problem is similar to the question you have linked. Call model.ToList() before using Where. This will force EF to materialize entities and then apply the rest of the filtering in memory.

public ViewResult Tag(string tag) {

    var _rawTag = HttpUtility.UrlDecode(tag);

    ViewBag.Tag = _rawTag;

    var model = _accommpropertyrepo.GetAllAccommPropertiesFullWebIgnoringApprovalStatus();

    var result = model.ToList().Where(
            x => x.AccommPropertyTags.Any(
                    y => y == _rawTag
                )
        );

    return View(result);
}
like image 101
Eranga Avatar answered Sep 22 '22 16:09

Eranga