在 efcore 中的方法中写入 from/group 而不是查询语法
Write from/group in method instead of query syntax in efcore
如何将此查询语法转换为方法语法?
var query =
from a in _dbContext.A
from b in a.B.DefaultIfEmpty()
from c in a.C.DefaultIfEmpty()
group new { b, c } by new
{
Id = a.Id.ToString(),
a.Name,
//...
} into g
select new SomeDto
{
Id = g.Key.Id,
Name = g.Key.Name,
//...
};
嗯,这在方法链语法中是这样的:
var uglyQuery = _dbContext.A
.SelectMany(a => a.B.DefaultIfEmpty(), (a, b) => new { a, b })
.SelectMany(t => t.a.C.DefaultIfEmpty(), (t, c) => new { t, c })
.GroupBy(x => new
{
Id = x.t.a.Id.ToString(),
x.t.a.Name,
//...
}, x => new { x.t.b, x.c })
.Select(g => new SomeDto
{
Id = g.Key.Id,
Name = g.Key.Name,
//...
});
请注意,此转换在上下文菜单 ReSharper
中可用 Convert to LINQ method chain.
如何将此查询语法转换为方法语法?
var query =
from a in _dbContext.A
from b in a.B.DefaultIfEmpty()
from c in a.C.DefaultIfEmpty()
group new { b, c } by new
{
Id = a.Id.ToString(),
a.Name,
//...
} into g
select new SomeDto
{
Id = g.Key.Id,
Name = g.Key.Name,
//...
};
嗯,这在方法链语法中是这样的:
var uglyQuery = _dbContext.A
.SelectMany(a => a.B.DefaultIfEmpty(), (a, b) => new { a, b })
.SelectMany(t => t.a.C.DefaultIfEmpty(), (t, c) => new { t, c })
.GroupBy(x => new
{
Id = x.t.a.Id.ToString(),
x.t.a.Name,
//...
}, x => new { x.t.b, x.c })
.Select(g => new SomeDto
{
Id = g.Key.Id,
Name = g.Key.Name,
//...
});
请注意,此转换在上下文菜单 ReSharper
中可用 Convert to LINQ method chain.