【问题标题】:cannot convert from 'string' to 'string[]' when using In clause with linq [closed]使用带有 linq 的 In 子句时无法从“字符串”转换为“字符串 []”[关闭]
【发布时间】:2021-02-09 18:01:33
【问题描述】:

这是我尝试过的查询,但出现了上述错误。 b 是一个逗号分隔的列表。图像显示了 b 的输出。

var CheckInHand = from item in PaymentList
                  where item.Id.In(b)          
                  select item.TotalAmt;

[图像是逗号分隔的列表,即 b] [1]:https://i.stack.imgur.com/Y39bd.png

【问题讨论】:

    标签: c# linq select where-clause in-clause


    【解决方案1】:

    需要明确的是,In 需要 string[],而您正在尝试传递 b,它是 string

    您可以使用String.Split 将逗号分隔的string 转换为string[]

    var CheckInHand = from item in PaymentList
                      where item.Id.In(b.Split(',', StringSplitOptions.RemoveEmptyEntries))
                      select item.TotalAmt;
    

    【讨论】:

    • 感谢它的工作。非常感谢。
    【解决方案2】:

    除了@dakota-methvin,这可能是相关的。

            [Fact]
            public void AssertItem()
            {
                var b = new int[] {311, 306, 299, 303, 304, 302, 301, 291, 281};
                var PaymentList = new Payment[] {new Payment() {Id = 311, TotalAmt = 500}};
                var CheckInHand = from item in PaymentList
                    where b.Contains(item.Id)
                    select item.TotalAmt.ToString("$##.00");
                Assert.Equal("$500.00",CheckInHand.First());
            }
    
            public class Payment
            {
                public int TotalAmt { get; set; }
                public int Id { get; set; }
            }
    

    【讨论】:

      猜你喜欢
      • 2022-08-10
      • 2010-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多