Select 带有子查询的查询 return List<string>
Select query with subquery that can return List<string>
我有两个 table:产品和产品图片
我正在尝试编写一个查询来返回产品,并为每个产品关联一个图像名称列表,但不断收到错误消息(如下所示)。
实体如下所示:
public class ProductDto
{
public int Id { get; set; }
public string Name { get; set; }
public long ProductNumber { get; set; }
public decimal? Size { get; set; }
public string SizeMeasurement { get; set; }
public decimal? Pack { get; set; }
public List<string> PhotoFileNames { get; set; }
}
此处显示出错的查询:
var query = from p in _productRepository.GetAll()
let PhotoFileNames = _productPhotoMapRepository.GetAll().DefaultIfEmpty()
.Where(e => e.ProductId == p.Id).Select(e => e.FileName).ToList()
where
(string.IsNullOrWhiteSpace(input.Filter) || p.Name.Contains(input.Filter))
select new
{
p.Id,
p.ProductNumber,
p.Name,
p.Size,
p.SizeMeasurement,
p.Pack,
//I've also tried doing this but get same error:
//PhotoFileNames = _productPhotoMapRepository.GetAll().DefaultIfEmpty()
// .Where(e => e.ProductId == p.Id).Select(e => e.FileName).ToList()
PhotoFileNames
};
var productList = await query.ToListAsync();
我收到的错误是:
System.ArgumentException: Expression of type 'System.Collections.Generic.IAsyncEnumerable`1[System.String]' cannot be used for parameter of type 'System.Collections.Generic.IEnumerable`1[System.String]' of method 'System.Collections.Generic.List`1[System.String] ToList[String](System.Collections.Generic.IEnumerable`1[System.String])'
Parameter name: arg0
at System.Dynamic.Utils.ExpressionUtils.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arguments, ParameterInfo pi, String methodParamName, String argumentParamName, Int32 index)
at System.Linq.Expressions.Expression.Call(MethodInfo method, Expression arg0)
at System.Linq.Expressions.MethodCallExpression1.Rewrite(Expression instance, IReadOnlyList`1 args)
at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalEntityQueryableExpressionVisitor.VisitMethodCall(MethodCallExpression node)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at System.Linq.Expressions.ExpressionVisitor.VisitAndConvert[T](ReadOnlyCollection`1 nodes, String callerName)
at Remotion.Linq.Parsing.RelinqExpressionVisitor.VisitNew(NewExpression expression)
at System.Linq.Expressions.NewExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at System.Dynamic.Utils.ExpressionVisitorUtils.VisitArguments(ExpressionVisitor visitor, IArgumentProvider nodes)
at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalEntityQueryableExpressionVisitor.VisitMethodCall(MethodCallExpression node)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
出于性能原因,我试图避免使用 for 循环。如果我完全省略 PhotoFileNames,产品 return 没有任何问题。
获得每个产品和存储在不同 sql table 中的所有图像文件名的预期结果的正确方法是什么?
编辑:添加了数据库上下文和模型 类
public class Product
{
public int Id { get; set; } //Aka productId
public string Name { get; set; }
public long ProductNumber { get; set; }
public decimal? Size { get; set; }
public string SizeMeasurement { get; set; }
public decimal? Pack { get; set; }
}
public class ProductPhotoMap
{
public virtual long ProductPhotoId {get;set;}
public virtual int DisplayOrder { get; set; }
public virtual bool IsDefault { get; set; }
public virtual int ProductId { get; set; }
public Product Product { get; set; }
public virtual string FileName { get; set; }
}
在我的上下文中:
public virtual DbSet<Product> Products { get; set; }
public virtual DbSet<ProductPhotoMap> ProductPhotoMappings { get; set; }
.ToListAsync()
不 return 一个 List<string>
。您可以更新 属性,或非异步使用 .ToList()
。
您的整个查询完全混乱。所以很难猜到你真正想要的是什么。但我假设您可能想要以下内容:
所以首先更新你的 Product
class 如下:
public class Product
{
public int Id { get; set; } //Aka productId
public string Name { get; set; }
public long ProductNumber { get; set; }
public decimal? Size { get; set; }
public string SizeMeasurement { get; set; }
public decimal? Pack { get; set; }
public ICollection<ProductPhotoMap> ProductPhotoMaps {get; set;}
}
然后写查询如下:
var query = _productRepository.GetAll().Select(p => new ProductDto
{
Id = p.Id,
ProductNumber = p.ProductNumber,
Name = p.Name,
Size = p.Size,
SizeMeasurement = p.SizeMeasurement,
Pack = p.Pack,
PhotoFileNames = p.ProductPhotoMaps.Select(pp => pp.FileName).ToList()
});
If(!string.IsNullOrWhiteSpace(input.Filter))
{
query.Where(p => p.Name.Contains(input.Filter))
}
var productList = await query.ToListAsync();
我有两个 table:产品和产品图片
我正在尝试编写一个查询来返回产品,并为每个产品关联一个图像名称列表,但不断收到错误消息(如下所示)。
实体如下所示:
public class ProductDto
{
public int Id { get; set; }
public string Name { get; set; }
public long ProductNumber { get; set; }
public decimal? Size { get; set; }
public string SizeMeasurement { get; set; }
public decimal? Pack { get; set; }
public List<string> PhotoFileNames { get; set; }
}
此处显示出错的查询:
var query = from p in _productRepository.GetAll()
let PhotoFileNames = _productPhotoMapRepository.GetAll().DefaultIfEmpty()
.Where(e => e.ProductId == p.Id).Select(e => e.FileName).ToList()
where
(string.IsNullOrWhiteSpace(input.Filter) || p.Name.Contains(input.Filter))
select new
{
p.Id,
p.ProductNumber,
p.Name,
p.Size,
p.SizeMeasurement,
p.Pack,
//I've also tried doing this but get same error:
//PhotoFileNames = _productPhotoMapRepository.GetAll().DefaultIfEmpty()
// .Where(e => e.ProductId == p.Id).Select(e => e.FileName).ToList()
PhotoFileNames
};
var productList = await query.ToListAsync();
我收到的错误是:
System.ArgumentException: Expression of type 'System.Collections.Generic.IAsyncEnumerable`1[System.String]' cannot be used for parameter of type 'System.Collections.Generic.IEnumerable`1[System.String]' of method 'System.Collections.Generic.List`1[System.String] ToList[String](System.Collections.Generic.IEnumerable`1[System.String])'
Parameter name: arg0
at System.Dynamic.Utils.ExpressionUtils.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arguments, ParameterInfo pi, String methodParamName, String argumentParamName, Int32 index)
at System.Linq.Expressions.Expression.Call(MethodInfo method, Expression arg0)
at System.Linq.Expressions.MethodCallExpression1.Rewrite(Expression instance, IReadOnlyList`1 args)
at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalEntityQueryableExpressionVisitor.VisitMethodCall(MethodCallExpression node)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at System.Linq.Expressions.ExpressionVisitor.VisitAndConvert[T](ReadOnlyCollection`1 nodes, String callerName)
at Remotion.Linq.Parsing.RelinqExpressionVisitor.VisitNew(NewExpression expression)
at System.Linq.Expressions.NewExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at System.Dynamic.Utils.ExpressionVisitorUtils.VisitArguments(ExpressionVisitor visitor, IArgumentProvider nodes)
at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalEntityQueryableExpressionVisitor.VisitMethodCall(MethodCallExpression node)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
出于性能原因,我试图避免使用 for 循环。如果我完全省略 PhotoFileNames,产品 return 没有任何问题。
获得每个产品和存储在不同 sql table 中的所有图像文件名的预期结果的正确方法是什么?
编辑:添加了数据库上下文和模型 类
public class Product
{
public int Id { get; set; } //Aka productId
public string Name { get; set; }
public long ProductNumber { get; set; }
public decimal? Size { get; set; }
public string SizeMeasurement { get; set; }
public decimal? Pack { get; set; }
}
public class ProductPhotoMap
{
public virtual long ProductPhotoId {get;set;}
public virtual int DisplayOrder { get; set; }
public virtual bool IsDefault { get; set; }
public virtual int ProductId { get; set; }
public Product Product { get; set; }
public virtual string FileName { get; set; }
}
在我的上下文中:
public virtual DbSet<Product> Products { get; set; }
public virtual DbSet<ProductPhotoMap> ProductPhotoMappings { get; set; }
.ToListAsync()
不 return 一个 List<string>
。您可以更新 属性,或非异步使用 .ToList()
。
您的整个查询完全混乱。所以很难猜到你真正想要的是什么。但我假设您可能想要以下内容:
所以首先更新你的 Product
class 如下:
public class Product
{
public int Id { get; set; } //Aka productId
public string Name { get; set; }
public long ProductNumber { get; set; }
public decimal? Size { get; set; }
public string SizeMeasurement { get; set; }
public decimal? Pack { get; set; }
public ICollection<ProductPhotoMap> ProductPhotoMaps {get; set;}
}
然后写查询如下:
var query = _productRepository.GetAll().Select(p => new ProductDto
{
Id = p.Id,
ProductNumber = p.ProductNumber,
Name = p.Name,
Size = p.Size,
SizeMeasurement = p.SizeMeasurement,
Pack = p.Pack,
PhotoFileNames = p.ProductPhotoMaps.Select(pp => pp.FileName).ToList()
});
If(!string.IsNullOrWhiteSpace(input.Filter))
{
query.Where(p => p.Name.Contains(input.Filter))
}
var productList = await query.ToListAsync();