我已经初始化了一个字符串数组,然后在foreach循环中尝试执行linq查询并将查询结果存储到已初始化的字符串数组中。
我尝试了ToList()之类的方法,但是它不起作用
public class TierImaegeDefinitionDTO
{
public string Tier { get; set; }
public string[] ImageDefinition { get; set; }
}
foreach (var tierVal in tierList)
{
String tier = tierVal.Tier;
var imageDefinitionList = (from p in _context.Product
join ti in _context.TierImageMap
on p.Id equals ti.ProductId join t
in _context.Tiers on ti.TierType
equals t.Id
where (p.Id == ProductId) &&
(t.TierType == tier)
select new
{
ti.ImageName,
}).ToList().Distinct();
await _context.SaveChangesAsync();
tierVal.ImageDefinition = imageDefinitionList;//getting error in this line
}
答案 0 :(得分:2)
ImageDefinition是字符串[]的数组。 ToList()将返回List
.Distinct().ToArray()
另一个问题是您正在选择一个对象,因此.ToList()实际上返回List <匿名类型>。相反,您只需选择ti.ImageName
var imageDefinitionList =
(from p in _context.Product
join ti in _context.TierImageMap on p.Id equals ti.ProductId
join t in _context.Tiers on ti.TierType equals t.Id
where (p.Id == ProductId) && (t.TierType == tier)
select ti.ImageName
).Distinct().ToArray();