I have a complex type named MSISDN which looks like below:
public class MSISDN
{
public string NDC { get; set; }
public string CC { get; set; }
public string SN { get; set; }
public string ToString() => $"{NDC}{SN}{CC}";
public string ToLong() => NDC * Math.Power(10, 6) + SN * Math.Power(10,3) + CC;
}
I also written an specific ValueConverter for this Complex type:
public class MsisdnValueConverter : ValueConverter<Msisdn, string>
{
public MsisdnValueConverter() : base(msisdn => msisdn.ToString(), msisdnString => Msisdn.Parse(msisdnString), mappingHints: null) { }
}
Which serialize/deserialize this complex type. I'm gonna write following query:
SubscriberCard card = cardDbSet.FirstOrDefault(P => P.Msisdn == msisdn);
This code throws following exception:
Unable to cast object of type 'System.String' to type 'System.Int64'.
I tried to use ToString() to avoid comparing objects as long as they are stored as string (or more precise NVARCHAR(MAX))
So in my next try I have used following query:
SubscriberCard card = cardDbSet.FirstOrDefault(P => P.Msisdn.ToString() == msisdn.ToString());
It throws following exception also:
could not be translated. Additional information: Translation of method 'object.ToString' failed. If this method can be mapped to your custom function, see https://go.microsoft.com/fwlink/?linkid=2132413 for more information. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information
The only feasible way to do this for me is to retrieve all the records into the memory by calling ToList(). but it costs a lot.
As the latest exception said I will try to write a DbFunction, Is there any better option for me ?
Properties which has conversion cannot be used in LINQ queries.
Define in
SubscriberCardproperty which holds real database string and map it to table's column. Also define propertyMsisdn, which can populateMSISDNobject from stringAnd use in queries
MsisdnStringproperty.