我在此时遇到“函数”错误,因为当我尝试将其从vb.net转换为c#时,由于其保护级别,无法访问system.data.function'

mMSH = mSplittxt.Where(Function, x, x.ToString().StartsWith("MSH|")).Select(Function, x, x).FirstOrDefault.ToString();


下面是我的代码,谢谢您的帮助。

        System.Net.Sockets.NetworkStream networkStream = default(System.Net.Sockets.NetworkStream);
        //-> some way it is called initialization or getting stream from the connected port.

        networkStream = HL7Server.GetStream();

        //-->check we have data
        //GetACK(networkStream, True)

        //-> write to net stream

        networkStream.Write(bytesSent, 0, bytesSent.Length);

        networkStream.Flush();


        //HL7Server.Close()
        hasConnected = false;

        //-> get current MSH segment
        dynamic mSplittxt = hl7message.Replace(System.Convert.ToChar(11).ToString(), "").Replace(System.Convert.ToChar(28).ToString(), "").Split(System.Convert.ToChar(13));
        //mMSH = mSplittxt.Where(x => x.ToString().StartsWith("MSH|")).Select(x => x).FirstOrDefault.ToString();
        mMSH = mSplittxt.Where(Function, x, x.ToString().StartsWith("MSH|")).Select(Function, x, x).FirstOrDefault.ToString();


        //MessageBox.Show("BEFORE ACK MSH: " & mMSH)

        //-> get acknowledgment.

        bool hasData = false;
        int retStat = 0;
        System.DateTime stTim = System.DateTime.Now;
        TimeSpan sp = System.DateTime.Now.Subtract(stTim);
        // wait for 5 seconds to get ack
        while (true)
        {
            hasData = GetACK(ref networkStream, false, ref retStat);
            sp = System.DateTime.Now.Subtract(stTim);
            // stay until 5 seconds or success acknowledge received
            if (sp.Seconds > 5 | hasData == true)
                break; // TODO: might not be correct. Was : Exit While

            Thread.Sleep(1000);

        }

最佳答案

该lambda语法对c#不正确

mMSH = mSplittxt.Where(x => x.ToString().StartsWith("MSH|"))
                .Select(x => x).FirstOrDefault().ToString();


可能可以更正它,以使您最终不会调用ToString(如果为null)。

mMSH = mSplittxt.Where(x => x.ToString().StartsWith("MSH|"))
                .Select(x => x.ToString()).FirstOrDefault();


甚至

mMSH = mSplittxt.Select(x => x.ToString())
                .Where(x => x.StartsWith("MSH|"))
                .FirstOrDefault();

08-20 00:49