gpt4 book ai didi

c# - 尝试循环访问 Azure 表中的实体时出错

转载 作者:行者123 更新时间:2023-12-03 01:57:51 25 4
gpt4 key购买 nike

当我尝试循环访问 Azure 表中的一堆实体时,我不断收到此错误“当前值‘String.Empty’类型与预期的‘System.Boolean’类型不兼容”只是刚开始使用 Azure,所以这可能是非常简单的事情,我收到的错误。

我的代码:

private void registerButton_Click(object sender, RoutedEventArgs e)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));

// Create the table client
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

// Get the data service context
TableServiceContext serviceContext = tableClient.GetDataServiceContext();

// Create a new customer entity
user = new UserDetailsEntity();

//Setting the fields of the new userEntity
user.username = usernameText.Text;
user.password = passwordText.Text;
user.subscriptionID = subText.Text;
user.subscriptionName = subscriptionNameText.Text;
user.thumbprint = thumbprintText.Text;
user.email = emailText.Text;
user.phoneNumber = "3530" + numberText.Text;


int rowCount = 1;


CloudTableQuery<UserDetailsEntity> Query = (from en in serviceContext.CreateQuery<UserDetailsEntity>("userdetails")
select en).AsTableServiceQuery<UserDetailsEntity>();

//error occurs in the next line
foreach (UserDetailsEntity ent in Query)
{

rowCount++;
}

user.RowKey = rowCount.ToString();





// Add the new customer to the people table
serviceContext.AddObject("userdetails", user);

// Submit the operation to the table service
serviceContext.SaveChangesWithRetries();
//Set the variables so they can be retrieved when the next screen loads
Application.Current.Properties["username"] = usernameText.Text;
Application.Current.Properties["password"] = passwordText.Text;

Window1 userHome = new Window1();
this.Close(); //to close Password window
userHome.Show(); //to show Main form
}

最佳答案

如果没有更多代码,我无法准确告诉您问题出在哪里,但是异常是相当有解释性的。您正在尝试将 bool 属性设置为字符串值。

如果您在代码注释中指出错误发生在您的 foreach 中,那么我将检查您的 UserDetailsEntity 对象是如何设置的。可能有一个属性设置为 bool 值,但您的数据返回为 String.Empty。您在 foreach 中获取此信息的原因是因为您的 LINQ 查询是 IQueryable 类型,因此在您实际访问数据(通过 foreach)* 之前,它不会实际执行并填充您的对象。因此,您可以在 UserDetailsEntity 属性中放置断点,看看它是哪一个(如果从代码来看这不是显而易见的)。

*请记住,这是 N+1 问题,您在循环的每次迭代中都会调用数据库。您可以通过调用 .ToList() 立即将所有数据加载到查询中来解决此问题...如果这对您来说是个问题。

关于c# - 尝试循环访问 Azure 表中的实体时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9943788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com