0

I have a set of data (Firstname, Lastname, Birthday) and I want to fetch from my database all the entries that match a set of these. Is there any way to do this via linq query?

I tried creating a compareclass:

public class CompareClass {
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public DateTime Birthday { get; set; }
}

And now my query looks like this:

var compareData = new List<CompareClass> {
    new CompareClass {
        Firstname = "Max",
        Lastname = "Power",
        Birthday = "2010-11-11"
    },
    new CompareClass {
        Firstname = "Marc",
        Lastname = "Power",
        Birthday = "2005-06-07"
    }   
};

var persons = await _context.Person
    .Where(p => comparedata.Contains(new CompareClass { 
        Firstname = p.Firstname, 
        Lastname = p.Lastname, 
        Birthday = p.Birthday }))
    .Select(p => new SomeOtherClass
    {
        Name = p.Lastname + " " + p.Firstname,
        Birthday = p.Birthday
    })
    .OrderBy(p => p.Name)
    .ToListAsync();

Unfortunately, this results in an error:

System.InvalidOperationException: The LINQ expression '...' could not be translated.

Is there a way to do this in a linq query, without having to query all the Person Data from the database? The table is rather big and I would much rather filter it before getting everything out of the database

Svyatoslav Danyliv
  • 21,911
  • 3
  • 16
  • 32
Chi
  • 1,320
  • 1
  • 14
  • 48
  • 1
    [Answer](https://stackoverflow.com/a/76270396/10646316) to similar question. Just use `PredicateBuilder`. EF Core do not supports JOIN to local collection and you have to build predicate by yourself. – Svyatoslav Danyliv May 17 '23 at 11:57
  • be prepared there might be another issue: Cannot implicitly convert type 'string' to 'System.DateTime' might use: DateTime.Parse("2005-06-07") in class. also, you can use .Except( on 2 lists or .Intersect( – Power Mouse May 17 '23 at 16:18

0 Answers0