EFCore access mysql cannot use Distinct() in sub query?

35 Views Asked by At

MySql8.0 .net7 EF7

My Entity

public class Blog
{
    [Key]
    public int ID { get; set; }

    public string BlogName { get; set; }
}

public class Author
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Blog> Blogs { get; set; }
}

My Controller

var rt = _context
    .Authors
    .Select(a => new
    {
        a.ID,
        //BlogNames = a.Blogs.Select(b => b.BlogName).Distinct(),//also
        BlogNames = a.Blogs.GroupBy(b => b.BlogName).Select(b => b.Key)
    });
var lst = rt.ToList();

Error picture

Error Message:

System.InvalidOperationException:“The LINQ expression 'OUTER APPLY ( SELECT b.BlogName FROM Blogs AS b WHERE a.ID == b.AuthorID GROUP BY b.BlogName ) AS t' could not be translated. 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.”

But when i change mysql to sql server,it's running,why?

I want run this code to get a list that contains the author whith unique blog names. when i use same code with sql server it's good.

0

There are 0 best solutions below