15

I'm developing a WCF RESTful web service with Entity Framework Code First.

I have a table Users with a lot of columns. I do this to get an specific user:

context.Configuration.ProxyCreationEnabled = false;
var users = from u in context.Users
            where u.UserId == userId
            select u;

On this table, there is a password column, and I don't want return this column.

How can I exclude password column from that select?

VansFannel
  • 45,055
  • 107
  • 359
  • 626
  • 1
    possible duplicate of [LINQ to SQL - How to select specific columns and return strongly typed list](http://stackoverflow.com/questions/1094931/linq-to-sql-how-to-select-specific-columns-and-return-strongly-typed-list) – Iłya Bursov Oct 19 '13 at 06:30
  • 1
    By the way, IMHO this is bad design. You should never exposed your entities to your UI, Web Service, etc. You should have a DTO (Data Transfer Object) which basically is a POCO with only those fields that you want to expose. – Pepito Fernandez May 23 '14 at 13:51

5 Answers5

14

Its sad to say but NO

You do not have option to directly exclude any particular column. You may go with lazy loading of columns.

The easiest and non-liking method would be to include columns which you want.

Digvijay Verma
  • 216
  • 1
  • 9
2

Specify each column that you do want in your select statement:

var users = from u in context.Users

        where u.UserId == userId

        select u.UserId, u.Watever, etc... ;
Jason Enochs
  • 1,436
  • 1
  • 13
  • 20
  • 1
    This syntax gives me a compile-time error "`;` expected". Did you mean to use the `new { u.UserId, u.Whatever, ... }` syntax for anonymous types? – Dai Dec 22 '14 at 19:43
  • 2
    No, you simply forgot the semicolon at the end or made a typo. Of course you don't type the "etc." shown in my example. after the last column listed, leave off the comma and terminate it with a semicolon... select u.UserId, u.AnotherColumn, u.LastColumn; – Jason Enochs Dec 23 '14 at 21:14
  • 2
    The way you typed it is rejected by the compiler. [Here](http://stackoverflow.com/a/6772286/75500) is how to select multiple columns. I'm updating that in your answer. – Shimmy Weitzhandler Apr 17 '15 at 00:17
2

another way like this,

   var users = from u in context.Users
                where u.UserId == userId
                select new 
                {
                    col1 = u.UserId, 
                    col2 = u.Watever
                }.ToList();
0

You can create more than one LINQ object per table. I'd create one with the field you need and one without. It makes CRUD operations more difficult though.

Brent Lamborn
  • 1,370
  • 3
  • 17
  • 37
0

Yes, you can run LINQ with certain columns excluded:

  1. Store list of all columns in an array of string. E.g.

    var arrayOfColNames = dbContext.Model.FindEntityType(typeof(TableName)) .GetProperties().Select(x => x.Relational().ColumnName).ToArray() )

  2. Remove the unwanted columns from the above array of string. E.g.

    arrayOfColNames = arrayOfColNames .Where(w => !w.Equals("password")).ToArray();

  3. Run your LINQ select using the above filtered array of strings : https://stackoverflow.com/a/45205267/19386398

bobt
  • 411
  • 3
  • 8