Right, not obeying God's commandments is the key to not knowing the Lord (See 1 John 2:3-4). That is why they did not know Jesus because they were working iniquity (or sin). That is why Jesus said to depart from me, you that work iniquity.
You have it backwards and are confusing cause and effect. Keeping God's commandments is the effect of knowing the Lord and not the cause. Failing to believe in the Lord Jesus Christ by trusting in Him as the ALL-sufficient means of their salvation is the cause of not knowing the Lord. (John 3:15,16,18; Acts 10:43; 11:17; 13:39; 16:31). Our relationship with the Lord begins the moment that we
BELIEVE.
John 1:12 - But as many as
received Him, to them He
gave the right to become children of God, to those who
believe in His name:
John 17:3 - And
this is eternal life, that they may
know You, the only true God, and Jesus Christ whom You have sent (which is an intimate, experiential knowledge, found only in a relationship). The term "know" implies intimate, experiential knowledge, through a relationship with Him, not merely theoretical knowledge.
1 John 2:3 - By this
we know that we have (past tense with ongoing present results)
come to know Him, (already know Him/already saved/demonstrative evidence) if we "keep" (Greek word "tereo" - guard, observe, watch over) His commandments. BTW to "keep" His commandments does not mean sinless, perfect obedience to all of His commandments 100% of the time, as sinless perfectionists who are suffering from a terminal case of self righteousness teach. (1 John 1:8-10)
1 John 3:4 says that the breaking of the law is sin.
Iniquity is sin.
So they are breaking God's laws.
That is why they are told to depart from Jesus.
Apart from the blood of Christ and the righteousness of God, the sins of these many people in Matthew 7:22-23 remains and that's why they are told to depart from Jesus. They have not placed their faith (belief, trust, reliance) in Jesus Christ for salvation but instead were trusting in their works for salvation. Apart from the blood of Christ and the righteousness of God, we would all be seen in the eyes of God as workers of iniquity because all have sinned and come short of the glory of God. (Romans 3:23; 6:23) Faith in Jesus Christ is the solution to our sin problem and not the law. (Romans 3:24-28; Galatians 2:16; Ephesians 2:8,9)
Romans 4:5 - But to him who does not work but
BELIEVES on Him who justifies the ungodly,
his faith is accounted for righteousness, 6 just as David also describes the blessedness of the man to whom
God imputes righteousness apart from works.
Romans 10:1 - Brethren, my heart's desire and prayer to God for Israel is that they may be saved.
2 For I bear them witness that they have a zeal for God, but not according to knowledge. 3 For they being ignorant of God's righteousness, and
seeking to establish their own righteousness, have not submitted to the righteousness of God. 4 For
Christ is the end of the law for righteousness to everyone who BELIEVES.
Philippians 3:9 - and be found in Him,
not having my own righteousness, which is from the law, but that which is through
faith in Christ, the righteousness which is from God by faith.