none
Select remake to LINQ (same table) with EF5 RRS feed

  • Question

  • Hi all!

    I have a select, and I want to create LINQ expression, please help me!

    Thank You!

    My select:

    public static DataTable LanguagesCombination()
            {
                string SQL = "SELECT l1.ID,l2.ID,l1.name,l2.name FROM languages l1 left join languages l2 on l1.id != l2.id order by l1.name";
                return msDbConnection.sqlCon.Query(SQL);
            }
    How can make LINQ?

    Sunday, February 9, 2014 2:07 PM

Answers

  • Hi Kugynok1;

    This code snippet should give you the results you are looking for.

    // Where DbContextInstance is the instance of the EF DbContext
    // Languages is the Language table in the database
    var results = from l1 in DbContextInstance.Languages
                  from l2 in DbContextInstance.Languages
                  where l1.ID != l2.ID
                  select new
                  {
                      l1ID = l1.ID,
                      l2ID = l2.ID,
                      l1Name = l1.Name,
                      l2Name = l2.Name
                  };

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by Fred BaoModerator Tuesday, February 18, 2014 3:21 AM
    • Unmarked as answer by Kugynok1 Friday, February 21, 2014 7:23 AM
    • Marked as answer by Kugynok1 Friday, February 21, 2014 7:23 AM
    Sunday, February 9, 2014 4:52 PM

All replies

  • Hi Kugynok1;

    Not sure what the function is doing but the SQL statement can be translated into Linq to EF as shown below.

    var query = from l_1 in DbContext.l1
                join l_2 in DbContext.l2 on l_1.id equals l_2.id
                orderby l_1.name
                select new 
                {
                    l1ID = l_1.id,
                    l2ID = l_2.id,
                    l1Name = l_1.name,
                    l2Name = l_2.name
                };   

      

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Sunday, February 9, 2014 2:38 PM
  • This languages table and I want load to languages combination in combobox.

    Example:

    1 English
    2 Hungary

    result

    English - Hungary
    Hungary - English

    etc.

    Sunday, February 9, 2014 3:09 PM
  • Sorry I do not understand what you want and I do not know the structure of the two tables and any relationships.

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Sunday, February 9, 2014 3:34 PM
  • One table.

    languages table
    id field
    name field

    My select languages join to languases, so same, nedd to all combination where l1.ID != l2.ID.

    Sorry my bad english. :S

    Sunday, February 9, 2014 4:24 PM
  • Hi Kugynok1;

    This code snippet should give you the results you are looking for.

    // Where DbContextInstance is the instance of the EF DbContext
    // Languages is the Language table in the database
    var results = from l1 in DbContextInstance.Languages
                  from l2 in DbContextInstance.Languages
                  where l1.ID != l2.ID
                  select new
                  {
                      l1ID = l1.ID,
                      l2ID = l2.ID,
                      l1Name = l1.Name,
                      l2Name = l2.Name
                  };

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by Fred BaoModerator Tuesday, February 18, 2014 3:21 AM
    • Unmarked as answer by Kugynok1 Friday, February 21, 2014 7:23 AM
    • Marked as answer by Kugynok1 Friday, February 21, 2014 7:23 AM
    Sunday, February 9, 2014 4:52 PM
  • Thank You! :)
    • Marked as answer by Kugynok1 Friday, February 21, 2014 7:26 AM
    • Unmarked as answer by Kugynok1 Friday, February 21, 2014 7:26 AM
    Sunday, February 9, 2014 6:42 PM
  • Hi Kugynok1;

    Did that resolve your issue? If it did please close the question out by accepting the solution.

    Thank you.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Sunday, February 9, 2014 8:57 PM
  • How can close? I dont find! :S
    Friday, February 21, 2014 7:27 AM