简体   繁体   English

CS0029 C#无法将类型隐式转换为'string []'

[英]CS0029 C# Cannot implicitly convert type to 'string[]'

I'm have initialised a string array,then inside a foreach loop I try to execute a linq query and store the result of the query to the string array that was initialised. 我已经初始化了一个字符串数组,然后在foreach循环中尝试执行linq查询并将查询结果存储到已初始化的字符串数组中。

I tried ToList() and such but it does'nt work 我尝试了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
            }

ImageDefinition is an array of string[]. ImageDefinition是字符串的数组[]。 ToList() will return List<T>, and Distinct will return IEnumerable<T>. ToList()将返回列表<T>,和鲜明将返回的IEnumerable <T>。 You need to change your call so that it ends with 您需要更改通话以使其以

.Distinct().ToArray()

Another problem is that you are selecting an object, so your .ToList() is actually returning List<anonymous type>. 另一个问题是您正在选择一个对象,因此您的.ToList()实际上返回的是List <匿名类型>。 Instead, you can just select ti.ImageName 相反,您可以只选择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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CS0029 C#&#39;无法将类型string []隐式转换为字符串&#39; - CS0029 C# 'Cannot implicitly convert type string[] to string' C#错误:无法将类型&#39;string&#39;隐式转换为&#39;System.Windows.Forms.Control&#39;(CS0029) - c# Error: Cannot implicitly convert type 'string' to 'System.Windows.Forms.Control' (CS0029) 错误 CS0029 无法将类型“字符串”隐式转换为“双精度” - Error CS0029 Cannot implicitly convert type 'string' to 'double' 错误 CS0029:无法将类型“string”隐式转换为“int” - Error CS0029: Cannot implicitly convert type 'string' to 'int' 错误CS0029无法将类型“字符串”隐式转换为“十进制” - Error CS0029 Cannot implicitly convert type 'string' to 'decimal' 错误 CS0029:无法将类型“int”隐式转换为“string” - Error CS0029: Cannot implicitly convert type 'int' to 'string' C# - 错误 CS0029 无法将类型“int”隐式转换为“Core.Models.Mandate” - C# - Error CS0029 Cannot implicitly convert type 'int' to 'Core.Models.Mandate' 我得到了 CS0029 C# 无法将类型“int”隐式转换为“bool” - i got CS0029 C# Cannot implicitly convert type 'int' to 'bool' CS0029 C#无法将类型&#39;void&#39;隐式转换为&#39;System.EventHandler&#39; - CS0029 C# Cannot implicitly convert type 'void' to 'System.EventHandler' CS0029 C# 无法将类型隐式转换为“int?” ASP.NET CORE 5 MVC 标识 - CS0029 C# Cannot implicitly convert type to 'int?' ASP.NET CORE 5 MVC Identity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM