Pergunta

I'm new to LINQ, and (sadly) i need to convert some messed sql command to Linq. There are two simple tables in my database. First one is holding names (cols "Name" and "NameID"), other table holds nicknames (cols "Nick", "NickID", "FK_NameID"). Each nickname has ID and foreign key pointing to real name (name ID).

Using C#, this code was used to retrive any nickname that contains "SomeString" in it:

comm = new SqlCommand("SELECT Name FROM Names INNER JOIN Nicks ON NameID = FK_NameID AND Nick LIKE '%" + SomeString + "%'", Connection);

As far as i saw after googling, "Like" and "Join" in SQL command could produce complicated Linq :\ Can you help me out and tell me what LINQ expression would be equal to that SQL command i was using so far? Is there any was to bypass Linq and use DataSet, DataTables instead? I'm total Linq-noob :\

Foi útil?

Solução

It's pretty similar actually:

var query = from name in names
            join nick in nicks on name.NameId equals nick.FK_NameId
            where nick.Nick.Contains(SomeString)
            select name.Name;

You should think about your column names - FK_NameId would be better as NameId (in my opinion).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top