简体   繁体   中英

How to select all columns plus a custom one from table by using EF LINQ?

How can I do this using LINQ in entity framework ?

SELECT TOP 1 
       'abc' as test
      ,T_Users.*
FROM T_Users
ORDER BY T_Users.BE_Name

I have this:

var initme = (
    from c in Repo.T_Users
    orderby c.BE_Name
    select new {
        test = "abc",
        c
    }
).FirstOrDefault();

But this gives:

object
{
   abc
  ,c
}

instead of

object
{
   abc
   ,c.col_1
   ,c.col_2
   ,c.col_3
   ...
   ,c.col_N
}

I don't think that is possible; I'm afraid you'll just have to use:

var initme = (from c in Repo.T_Users
                          orderby c.BE_Name

                          select new
            {
                 test =  "abc",
                 c1 = c.col_1, 
                 c2 = c.col_2,
                 ... 
            }

You have to manually specify all columns in your select clause in LINQ, there is no shorter way.

var initme = (from c in Repo.T_Users
              orderby c.BE_Name
              select new
              {
                 test =  "abc",
                 col1 = c.col_1,
                 col12 = c.col_2
                 /....
              }
             ).FirstOrDefault();

You have to specify all the fields you need in the select. Or you can create a custom object like repoDto that has all properties the properties of repo table plus your test field that you're going to populate after the query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM