0

以下查询可以正常工作并填充其下拉列表。数据库中的数据全部以大写形式存储,即PALM BEACH。我想将它转换为正确的情况,显然我可以通过遍历返回的列表并重新格式化来完成,但我应该能够在查询本身中做到这一点。以下查询工作正常。

 Dim citylist As List(Of String) = (From c In ctx.ziptaxes
                                                   Where c.StateID = ddlStates.SelectedIndex                                                      
                                                   Order By c.City Ascending
                                                   Select c.City).ToList()

但是如果我尝试将它转换成这样的东西,它会失败

 Dim citylist As List(Of String) = (From c In ctx.ziptaxes
                                                   Where c.StateID = ddlStates.SelectedIndex
                                                   Let cityname = StrConv(c.City, VbStrConv.ProperCase)
                                                   Order By cityname Ascending
                                                   Select cityname).ToList()

我也尝试过使用文化信息和 String.Format(c.City, vbProperCase) ,除了原始查询之外别无他法。任何帮助表示赞赏。

附录: 一些进一步的研究告诉我,在运行查询之前不能使用 .Net 对象,如字符串转换和cultureinfo。如果是这种情况,它解释了为什么它不起作用。以下解决了我的问题,但我仍然想知道在 LINQ to EF 中是否有办法做到这一点。

 Dim citylist As List(Of String) = (From c In ctx.ziptaxes
                                                   Where c.StateID = ddlStates.SelectedIndex
                                                   Order By c.City Ascending
                                                   Select c.City).ToList()
                If citylist.Count > 0 Then
                    For i As Integer = 0 To citylist.Count - 1
                        citylist(i) = StrConv(citylist(i).ToLower(), vbProperCase)
                    Next
                    With ddlCity
                        .Items.Clear()
                        .DataSource = citylist.Distinct()
                        .DataBind()
                        .Items.Insert(0, "Select a city")
                        .SelectedIndex = 0
                    End With
                End If
4

1 回答 1

0

您可以在 SELECT 中进行转换。这是一个示例(使用过度简化的城市名称转换器):

using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;

namespace LinqQuestion
{
    [TestFixture]
    public class StackOverflowTests
    {
        private IEnumerable<City> _cities;

        [TestFixtureSetUp]
        public void Arrange()
        {
            _cities = new List<City>
            {
                new City { Id = 1, Name = "FLINT", StateId = 1 },
                new City { Id = 2, Name = "SAGINAW", StateId = 1 },
                new City { Id = 3, Name = "DETROIT", StateId = 1 },
                new City { Id = 4, Name = "FLint", StateId = 1 }
            };
        }

        [Test]
        public void TestCountryQuery()
        {
            var data = _cities
                .Where(c => c.StateId == 1)
                .OrderBy(c => c.Name)
                .Select(c => StrConv(c.Name))
                .Distinct().ToList();

            Assert.That(data.Count == 3);
        }

        private static string StrConv(string original)
        {
            var firstLetter = original.Substring(0, 1).ToUpper();
            var theRest = original.Substring(1, original.Length - 1).ToLower();
            return firstLetter + theRest;
        }
    }

    public class City
    {
        public int Id { get; set; }
        public int StateId { get; set; }
        public string Name { get; set; }
    }
}
于 2013-08-18T16:55:30.440 回答