score:0
This will do the trick:
IQueryable<Animal> query =
from animal in context.Animals
where animal.AnimalAliases
.Any(a => a.Alias.Contains("USERINPUT"))
select animal;
Alternatively, you can do it the other way around (start at the AnimalAlias
entity instead of starting at the Animal
entity):
IQueryable<Animal> query = (
from animalAlias in context.AnimalAliases
where animalAlias.Alias.Contains("USERINPUT")
select animalAlias.Animal)
.Distinct();
score:3
I think what you're looking for is just a normal WHERE clause?
var query = from animal in context.Animals
join animalAlias in context.AnimalAliases on animal.AnimalID equals animalAlias.AnimalID
where animalAlias.Alias.Contains(USERINPUT)
select animal;
The alias text is not part of the foreign key relationship - so it shouldn't be in the join.
Update - after comments - including from @Steven - while the query above is correct and does mimic the original SQL - it might be wise to use Distinct()
on the output - this will prevent the case where multiple alias matches could occur for a single animal.
var query = (from animal in context.Animals
join animalAlias in context.AnimalAliases on animal.AnimalID equals animalAlias.AnimalID
where animalAlias.Alias.Contains(USERINPUT)
select animal).Distinct();
Source: stackoverflow.com
Related Articles
- How to write SQL SELECT INNER JOIN with multiple conditions (with LIKE) query to LINQ to SQL
- How to write a LINQ query with inner join conditions and IN sub query
- write linq similiar where in select from with inner join in sql query
- LINQ to Objects: Query with multiple LIKE conditions (OR or AND) possible?
- Entity Framework Query with multiple join conditions
- Select the most recent date in a LINQ query with an inner join
- LINQ query with Inner Join not working when multiple records are added to database
- How to write join query with multiple column - LINQ
- LINQ to SQL - Left Outer Join with multiple join conditions
- LINQ Join with Multiple Conditions in On Clause
- How to write linq query to match SQL like select top 100 * from tab?
- lambda expression join multiple tables with select and where clause
- How to use join with multiple conditions in linq-to-Nhibernate
- Linq to Entity Join table with multiple OR conditions
- LINQ Join with multiple AND conditions
- LINQ query with a WHERE clause with multiple conditions
- Multiple Select and Join with LINQ and Lambda
- Using LINQ left join with multiple conditions and subquery
- Query expressions over source type 'dynamic' or with a join sequence of type 'dynamic' are not allowed
- Greater Than with multiple conditions in Linq Join
- Group join with multiple conditions and one of them being with a constant
- Convert sql query with multiple inner joins to LINQ
- Linq query equivalent of exists with multiple conditions
- Linq query with two joins on inner select in EF
- Fluent LINQ query with multiple join conditions, one of which being a simple equals and another a less than comparison between ids
- TSQL - Select from table with multiple join paths
- EF core join with multiple OR conditions
- Multiple JOINS with Conditions between them : LINQ query
- LINQ query with multiple conditions in WHERE clause
- asp.net LinqDataSource query multiple tables with join
- LINQ Left Join SQL equivalent via DefaultIfEmpty fails to return results
- Passing delegate to linq query in EF Core
- How to check value overlap using LINQ
- Finding a row in a datatable that is the closest to my parameters
- Linq to Entities error
- Checking if item is null or if a table doesn't contain any matching data
- Case insensitive string compare in LINQ expression
- Combining Lists in Lambda/LINQ
- How to group by one member and select a second member?
- Catching exception in ToDictionary call
- Left join to a table which may or not contain the data
- C# LINQ Group by
- How do I get month names from this Linq group by query?
- How do I linq like query with Dictionary in typescript?
- Intersect two generic lists by dynamic properties
- Multiple context queries inside a query
- Uncommon elements in multiple lists
- LINQ - double grouping with elements counting
- LINQ group list and combine collections
- Converting Linq to XSLT