-1

I want to do this but it's not working when I want to use the except operator:

Duplicate of this post: (Retrieve an object from entityframework without ONE field)

and this : Exclude a field/property from the database with Entity Framework 4 & Code-First

using (var db = new DEntities())
{
    var ticket = db.Tickets
        .Include(o => o.Ticket)
        .Except(o => o.AttachmentFile)
        .Where(o => o.TicketID == ticketID).ToList();
}

I want to get all the table without the AttachmentFile.

Community
  • 1
  • 1
billybob
  • 2,859
  • 6
  • 35
  • 55
  • 1
    Define "not working" are you getting a result you don't expect or a Exception. If its a exception please [edit your original question](http://stackoverflow.com/posts/18218666/edit) and put a copy of the exception details there. There is a button that says "[Copy Exception Details to Clipboard](http://blogs.msdn.com/blogfiles/saraford/WindowsLiveWriter/Didyouknowyoucancopytheexceptiondetailsw_F67C/image_2.png)" that will let you copy it easily – Scott Chamberlain Aug 13 '13 at 20:34
  • Useless to downvote. I'm asking a question and getting a downvote. – billybob Aug 13 '13 at 20:50
  • 1
    (I was not the downvoter) A downvote represents: "*This question does not show any research effort; it is unclear or not useful*". I agree with the downvoter that your question falls under the "it is unclear" category. "*I want to get all the table without the AttachmentFile.*" sounds to me like you want the results where AttachmentFile is null. Also your attempted solution reinforces that assumption too. – Scott Chamberlain Aug 13 '13 at 21:02
  • See Edit post: I have the same problem as the linked post – billybob Aug 13 '13 at 21:04

2 Answers2

7

Except is a set subtraction operator - you use it by supplying elements of the same kind as the original set to request the removal of all elements of the Except set from the original set.

This is not what you are trying to achieve here - your task appears to be a simple filtering:

ticket = db.Tickets
    .Include(o => o.Ticket)
    .Where(o => o.AttachmentFile == null && o.TicketID == ticketID)
    .ToList();

This excludes all tickets with attachments by requiring the AttachmentFile to be null.

(Comment) I just want to ignore the o.attachmentFile I don't want to get it if it's null.

That's different: EF does not support lazy loading of individual properties, so unless the attachment is split in its own table (which I would recommend doing if you often query tickets without their attachments) you should select only the fields that you want to get:

ticket = db.Tickets
    .Include(o => o.Ticket)
    .Where(o => o.TicketID == ticketID)
    .Select(o => new MyObject(o.Attr1, o.Attr2, /*and so on, but no AttachmentFile */))
    .ToList();
Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I just want to ignore the o.attachmentFile I don't want to get it if it's null. Am I miss understanding? I want to set it to null and don't take the value of the database. – billybob Aug 13 '13 at 20:37
  • @billybob: You'll either need to create new objects without the `AttachmentFile` or loop through your objects and set them to `null`. – zimdanen Aug 13 '13 at 20:41
  • 1
    @billybob EF does not support lazy loading of individual properties. If you want your objects to come with no attachment, select the fields that you want, and do not include the attachment in the list. – Sergey Kalinichenko Aug 13 '13 at 20:41
3

You can use a select statement.

    var ticket = db.Tickets
                   .Include(o => o.Ticket)
                   .Where(o => o.TicketID == ticketID)
                   .Select( o => new { o.TicketID, o.Column1, o.Column2 }).ToList();

Replacing o.Column1, o.Column2, etc. with the appropriate column names. This will return an anonymous object containing the specified columns.

Mr Jones
  • 1,188
  • 3
  • 17
  • 33