0

I have a metadata class with some custom properties for easier data access. One problem that I have run across though, is that when I try to use this property in LINQ statements I get this error.

The specified type member 'CrewCodesStr' is not supported in LINQ to Entities.

I know why I get it, I'm wondering if there is another way to do this to allow the property to be used in LINQ statements.

Here is the property:

public string CrewCodesStr
{
    get { return string.Join(", ", CrewCodesList); }
}

Here is an example query that it fails on.

query = query.Where(wo => searchCriteria.crewCode.Contains(wo.CrewCodesStr));
JB06
  • 1,881
  • 14
  • 28
  • can you post the full query you're running – DLeh Oct 28 '15 at 18:52
  • 1
    Seems like the real question is why you're representing the same information in multiple fields on your model. If your data structure was normalised, you wouldn't need logic in the model at all... – Basic Oct 28 '15 at 18:58
  • 1
    Rather odd example that doesn't seem to be very useful. If your search criteria is "B,D", it would fail if CredCodeStr was "A,B,C,D" which you would think that is a match. – Robert McKee Oct 28 '15 at 19:13
  • Just rewrite your query as `query=query.Where(wo=>searchCriteria.crewCode.All(cc=>wo.CrewCodesList.Contains(cc)));` or `foreach(var cc in searchCriteria.crewCode) query=query.Where(wo=>wo.CrewCodesList.Contains(cc));` – Robert McKee Oct 28 '15 at 19:15
  • @RobertMcKee You're right, this was a pretty useless example, turns out it was old code that I hadn't gotten around to updating yet. A simple example would be `var wo = query.Where(x => x.CrewCodesStr.Contains("1234");` This was failing because I hadn't put the data in memory yet. – JB06 Oct 28 '15 at 20:52

1 Answers1

3

You can do a string.Join after you've selected the info you need from your database. If you do a ToList(), this will enumerate the data you need from the database. Then you can do whatever C# you want, as it will run in memory after the ToList()

var result = MyObjects.Select(x => new
{
    ID = x.ID,
    CrewCodes = x.CrewCodes
})
.AsEnumerable()//enumerate the queryable
.Select(x => new
{
    ID = x.ID,
    String = string.Join(",",x.CrewCodes)
});
DLeh
  • 23,806
  • 16
  • 84
  • 128
  • This is what I ended up using, but instead of doing the join here, I am able to call the CrewCodesStr property since the object is in memory now, and I'm working with LINQ to Objects instead of Entities. Thanks! – JB06 Oct 28 '15 at 20:53