0
{
"Id": 1234,
"CommentType": "project",
"EntityReferenceId": "1345-154-154",
"Members": [{
    "MemberId": "1354",
    "Name": "a",
    "Email": "[email protected]"
}],
"Threads": [{
        "Id": "233",
        "UserReferenceId": "32343",
        "UserName": "433434",
        "CommentByUserType": "Agent",
        "Content": "dfdfdsfs sfdf sdf",
        "PostedDate": "0001-01-01T00:00:00",
        "Active": true,
        "Attachment": [{
            "AttachmentName": "ad",
            "AttachmentUrl": "http://fdf.jpg"
        }]
    },
    {
        "Id": "233",
        "UserReferenceId": "32343",
        "UserName": "433434",
        "CommentByUserType": "Agent",
        "Content": "dfdfdsfs sfdf sdf",
        "PostedDate": "0001-01-01T00:00:00",
        "Active": false,
        "Attachment": [{
            "AttachmentName": "ad",
            "AttachmentUrl": "http://fdf.jpg"
        }]
    }]
}

I am using MongoDb to linq, This is my Comment" object format.for each Project there is a comment object.Each Comment object contains a list of "Threads"(you can see this above example).I want to load the "Comment" object with all thread which is Active("Active": true)

var result = _context.Comments
                     .AsQueryable()
                     .Where(x => x.EntityReferenceId == EntityReferenceId &&
                                 x.CommentType == Type && 
                                 x.Threads.Any(z=>z.Active==true))
                     .FirstOrDefault();

I used this query but it loading all threads if Any thread have "Active" value is true.

 x.Threads.Any(z=>z.Active==true) 

returns only bool values.I need a solution please

3
  • Have you tried to replace Any with Where and see the result? Commented Mar 13, 2018 at 5:14
  • I think we cant use "Where".Operator && cannot be applied to operands of type bool and IEnumerable<Threads> Commented Mar 13, 2018 at 5:18
  • That condition will not work cause you getting a master object based on child condition and master object contain multiple child you are not filtering child. Commented Mar 13, 2018 at 7:18

1 Answer 1

2

You can use x.Threads.Any(z=>z.Active==true) be where condition. or you will judge Threads collection is contain Active==true data.

A simple way you can do like this.

var result = _context.Comments
         .AsQueryable()
         .Where(x => x.EntityReferenceId == EntityReferenceId &&
                     x.CommentType == Type)
         .FirstOrDefault();

if (result!=null)
{
    result1.Threads = result1.Threads.Where(z => z.Active == true);
}

Or

use select method

var result = _context.Comments
                     .AsQueryable()
                     .Where(x => x.EntityReferenceId == EntityReferenceId &&
                                 x.CommentType == Type)
                     .Select(o => new Comment{
                        Id = o.Id,
                        Members = o.Members,
                        EntityReferenceId = o.EntityReferenceId,
                        CommentType = o.CommentType,
                        Threads = o.Threads.Where(z => z.Active == true)
                     })
                     .FirstOrDefault();
Sign up to request clarification or add additional context in comments.

2 Comments

I think this is not a proper way,why because,in the first query it will take the whole threads even if it is not active.if there is big amount of inactive comments it will also load.by cause of this it will be slow.
But this Downcasting is not supported ,I am using mongoDb database

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.