Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contains in Linq #35

Open
adria-arquimbau opened this issue Nov 22, 2022 · 2 comments
Open

Contains in Linq #35

adria-arquimbau opened this issue Nov 22, 2022 · 2 comments

Comments

@adria-arquimbau
Copy link

I have an issue and don't know how to solve it. Having 4 string fields from one entity I decided to change one of them "SerialNumber" to a value object.

At some point having an IQueryable I need to filter for the items that contain a similar search term, in this example details. There is no problem with using .Contains for any string fields but when I change SerialNumber to ValueObject and I don't have the Contain method anymore.

return query.Where(x =>
                x.Number.Contains(details) ||
                x.SerialNumber.Value.Contains(details) || //New field as string ValueObject
                x.Name.Contains(details) ||
                x.CarrierName.Contains(details));

I tried to use x.SerialNumber.Value.Contains(details), details.Contains(x.SerialNumber.Value) and some other approaches. And responding with the following error: Could not be translated

@adria-arquimbau adria-arquimbau changed the title Contain Operator in Linq Contains in Linq Nov 22, 2022
@mcintyre321
Copy link
Owner

This might help #28

@TheM1Stery
Copy link

TheM1Stery commented Jan 29, 2023

My solution was to create explicit conversion from ValueOf object to a primitive and after that use it like this:

var entity = await _dbContext.Objects.SingleOrDefaultAsync(x => (string)x.Id! == req.Id);

This piece of code translated fine and worked like intended
You can even create interface and implement it in your ValueOf classes(this uses new static abstract methods of c# 11):

public interface IConvertible<in T, out TTo> where T: IConvertible<T, TTo>
{
    static abstract explicit operator TTo(T obj);
}

You would still need to use value converter that is in the previous comment(don't need it for linq) for full ValueOf support though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants