Let's consider that charge \(q\) is distributed uniformly throughout a line as shown in Figure 1 and we are going to calculate the electric field at a point \(p\) which lies at a perpendicular distance \(x\) from the middle point of the line. It's always easier to start with a coordinate system and the associated symmetry with the charge distribution.

We consider infinitesimal length element of length \(dy\) on the line of charge. If the linear charge density is denoted by \(\lambda\) which is the charge per unit length, the charge associated within the length \(dy\) is \(dq =\lambda dy\). The line of charge is divided by the x-axis into equal halves of length \(l\), so the total length of the line of charge is \(2l\). The charge \(dq\) within the length \(dy\) is \(dq=\lambda dy=\frac{qdy}{2a}\) and the electric field \(dE\) due to this charge at point \(p\) is

\[dE = k\frac{{dq}}{{{r^2}}} = k\frac{{\lambda dy}}{{({x^2} + {y^2})}} = k\frac{{qdy}}{{2l({x^2} + {y^2})}}\]

Note that \(r^2 = x^2 + y^2\) in the above equation. The x-component of the electric field \(dE\) is \(dE_x=dE\cos \theta \) and the y-component is \(dE_y=-dE\sin \theta \) (why negative sign?). Note that the y-component of electric field produced by the upper half part of the line of charge is exactly equal in magnitude and opposite in direction to the y-component of electric field produced by the lower half part of the line of charge and therefore the y-component of the electric field at the point \(p\) due to the line of charge is zero. We can understand it by the symmetry consideration of the line of charge at the point \(p\). So the total electric filed due to the line of charge at that point is sum of the x-components of electric fields of all charge elements. Since \(\cos \theta =\frac{x}{r}=\frac{x}{\sqrt{{{x}^{2}}+{{y}^{2}}}}\), the x-component of \(dE\) is,

\[d{E_x} = dE\cos \theta = k\frac{{qxdy}}{{2l{{({x^2} + {y^2})}^{\frac{3}{2}}}}}\]

Now the total electric field at the point \(p\) due to the line of charge is determined by integrating \(dE_x\) from \(-l\) to \(l\). Which is

\[{E_x} = E = k\frac{{qx}}{{2l}}\int\limits_{ - l}^{ + l} {\frac{{dy}}{{{{({x^2} + {y^2})}^{\frac{3}{2}}}}}} = k\frac{q}{{x\sqrt {{x^2} + {l^2}} }} \tag{1} \label{1}\]

The \(x\) coordinate of the point is constant in the integraion. The unit vector \(\hat{i}\) gives the direction along positive x-axis, so the electric field in vector form is

\[\vec E = k\frac{q}{{x\sqrt {{x^2} + {l^2}} }} \hat i \tag{2} \label{2}\]

You can also check that the integration of \(d{{E}_{y}}\) from \(-l\) to \(l\) is zero:

\[d{E_y} = - dE\sin \theta = - k\frac{{qydy}}{{2l{{({x^2} + {y^2})}^{\frac{3}{2}}}}}\]

Now integrating \(d{{E}_{y}}\) from \(-l\) to \(l\),

\[{E_y} = - k\frac{q}{{2l}}\int_{ - l}^l {\frac{{ydy}}{{{{({x^2} + {y^2})}^{\frac{3}{2}}}}}} = 0 \tag{3} \label{3}\]

Only the sum of the x-components of electric fields produced by all elements of the line of charge gives the total electric field at the point \(p\).

When the point \(p\) is very far from the line of charge that is, the distance \(x\) is considerably larger in comparison to the length of the line of charge or symbolically \(x >> l\). The term \(l^2\) will be much smaller and can be neglected. So the electric field in this case is

\[E = k\frac{q}{{x\sqrt {{x^2}} }} = k\frac{q}{{{x^2}}} \tag{4} \label{4}\]

In this case when the point \(p\) is far from the line of charge the entire charge in the line acts as if all the charge were concentrated at the origin of the coordinate system (at the middle point of line of charge)

And when the line of charge is infinitely long, that is the length of the line of charge is very long in comparison to the distance \(x\), (\(l >> x\)), the distance \(x\) will be much smaller than \(l\) and \(x^2\) can be neglected. So, the resulting electric field is

\[E = k\frac{q}{{x\sqrt {{l^2}} }} = k\frac{q}{{lx}} \tag{5} \label{5}\]

As you know the linear charge density is \(\lambda =\frac{q}{2l}\) and in terms of the linear charge density, the above expression can be written as,

\[E = k\frac{{2\lambda }}{x} \tag{6} \label{6}\]

So the result is clear that the electric field for the infinitely long line of charge is proportional to \(\frac{1}{x}\) instead of \(\frac{1}{{{x}^{2}}}\) as for a point charge. And notice that we can not get a real line of charge having infinite length! But when the length of the line of charge is considerably larger in comparison to the distance \(x\) and the point \(p\) is considerably close enough to the line of charge the electric field is the same as that of an infinite line of charge.