foreach迭代变量错误在我的代码中给出.如何修改编码? [英] foreach iteration variable Error is giving in my code. how do I modify the coding ?

查看:87
本文介绍了foreach迭代变量错误在我的代码中给出.如何修改编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有以下代码.我在代码的最后一行遇到错误.错误是:-无法分配给文件",因为它是一个foreach迭代变量.请告诉我如何将代码更改为成功完成


hello I have the following code.I am facing the error in the last line of the code . Error Is :- can not assign to ''file'', because it is a foreach iteration variable. Please tell me how to change the code to successfully done


 string[] filesHD = Directory.GetFiles(dirPath, filepattern1);
            string[] filesDT = Directory.GetFiles(dirPath, filepattern2);
            string[] filesTD = Directory.GetFiles(dirPath, filepattern3);

List<string> lstAllFiles = new List<string>();
            lstAllFiles.AddRange(filesHD);
            lstAllFiles.AddRange(filesDT);
            lstAllFiles.AddRange(filesTD);
            string[] strAllFiles = lstAllFiles.ToArray();



foreach(string file in strAllFiles)
{
    foreach(char c in file)
    {
        if (c > 127)
        {
            using (SqlConnection con = new SqlConnection(CS))
            {
                con.Open();
                SqlCommand sqlcom = new SqlCommand("sp_ReplaceNonAsciiCharset", con);
                sqlcom.CommandType = CommandType.StoredProcedure;
                SqlParameter sqlparam = new SqlParameter();
                sqlparam.ParameterName = "@non_ascii_char";
                sqlparam.Direction = ParameterDirection.Input;
                sqlparam.SqlDbType = SqlDbType.NChar;
                sqlparam.Size = 2;
                sqlparam.Value = c;
                sqlcom.Parameters.Add(sqlparam);
                object o = sqlcom.ExecuteScalar();
                int i = file.IndexOf(c);
                file1 = file.Remove(i, 1);
                file2 = file1.Insert(i, o.ToString());
                file = file2;
            }

推荐答案

您正试图从foreach列表中删除项目,这将改变循环的行为.由于似乎没有对file1做任何事情,因此可以安全地删除此行,然后循环就可以了.

我没有注意到file = file2的分配.恐怕那还是行不通的.
You are attempting to remove items from the foreach list, which would change the behaviour of the loop. As you don''t seem to be doing anything with file1, it looks like you can safely remove this line and your loop should work then.

I hadn''t noticed the file=file2 assignation. That doesn''t work either I''m afraid.


只要把这行删掉:
Just take the line out:
file = file2;

在foreach循环内,您无法以任何方式更改迭代变量-它在编译器的控制之下,始终具有它的值,而不是您的值! />
但是...坦率地说,那是您要去的一些奇怪的代码.
到底为什么要通过对小型ASCII字符集之外的每个字符调用SQL来做到这一点?你知道那是多么低效吗?
如果必须通过SQL进行操作(个人而言,我不会),则将整行作为SQL中的一个对象进行处理,不要在这里玩一个字符,在那里玩一个字符.
但坦率地说,我会在C#中执行此操作(根本不是SQL),如果您使用Linq,它可能是整个字符串的一行代码...

You cannot change the iteration variable in any way while inside a foreach loop - it is under the compilers control what value it has at all times, not yours!

But...frankly that is some weird code you have going there.
Why on earth are you doing this by calling SQL for each and every character outside the small ASCII set? Do you have any idea how inefficient that is?
If you must do it via SQL (And personally, I wouldn''t) then do the whole line as one object in SQL, don''t play about with a character here, a character there.
But frankly, I''d do it in C# (not SQL at all), where it''s probably one line of code for the whole string if you use Linq...


从技术上讲,您可以使用另一种循环来解决该问题,例如for:
Technically you overcome that using another kind of loop, fo instance a for:
for ( int i=0; i<strAllFiles.Count; ++i)
{
    string file = strAllFiles[i];
    
    foreach(char c in file)
    {
        if (c > 127)
        {
            using (SqlConnection con = new SqlConnection(CS))
            {
               //...
               strAllFiles[i] = file2;



如果这确实有意义,则取决于您.



If that really makes sense, it is up to you.


这篇关于foreach迭代变量错误在我的代码中给出.如何修改编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆