2

I am using EF with the following definitions:

public class StoredFile
{
    public int StoredFileId { get; set; }
    public HashCode HashCode { get; set; }
    public string LocalName { get; set; }
    //and some more fields here...
}

and

[ComplexType]
public class HashCode
{
    public Byte [] ValueArr {get; set;}
    public override bool Equals(object o) {...}
    //some more methods to manipulate hash codes                
}

I am trying to get the following query to work:

    public bool TryGetFileInfo(MachineSafeDbDataContext dataContext, HashCode hash, out StoredFile fileInfo)
    {
        var matches = from curr in dataContext.StoredFiles where (hash.Equals(curr.HashCode)) select curr;

        if (matches.Count() == 0)
        {
            fileInfo = null;
            return false;
        }

        fileInfo = matches.First();
        return true;
    }

However I get the "Unable to create a constant value of type ..." exception. I guess LINQ fails to find a translation to SQL for the above WHERE statement.

I am aware of the following question: Entity Framework - "Unable to create a constant value of type..." exception, and of the following page by Microsoft: Known Issues and Considerations in LINQ to Entities. However, I would still like to select a file from my DB based on its hash code.

Thanks,

Edit: I should have gone over L2E limitations. This thread has some good pointers and links.

Community
  • 1
  • 1
OSH
  • 2,847
  • 3
  • 25
  • 46

1 Answers1

2

You can't call your overridden .Equals() in L2E.

You can, however, drill into the ValueArr directly:

var matches = from curr in dataContext.StoredFiles 
              where (curr.HashCode.ValueArr == hash.ValueArr) 
              select curr;
Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273