简体   繁体   中英

Where condition in linq or lambda query issue

This is the inital query.

var DevUsers = db.UserProfiles.Include("Tasks").Include("Projects").Include("FollowerTasks").Select(i => new
        {               
            Tasks = db.Tasks.Where(j => j.AssignedToPersonID == i.PersonID).Where(k => k.QAStatus != "Passed").Select(k => new
            {
                k.Projects,
                k.TaskName,
                k.ViewedByDeveloper,
                k.Status,
                k.QAStatus,
                k.ReleaseStatus,
                k.TaskID,
                k.DisplayTaskID,
                k.EstimatedDeliveryDate,
                k.AssignedToPerson.FirstName,
                Tags = k.Tags.Where(p => p.TagType == "General"),
                Modules = k.Tags.Where(p => p.TagType == "Module"),
                CodeTables = db.CodeTables.Where(l => l.Status == k.Status).FirstOrDefault(),
            }).OrderBy(k => k.ViewedByDeveloper).ThenBy(k => k.CodeTables.DisplayOrder).ThenByDescending(k => k.ReleaseStatus),
            i.PersonID,
            i.FirstName,
            i.LastName,
            i.UserID,
            i.EmailAddress,
        }).OrderBy(i => i.FirstName);

I cannot do any type of query in this DevUsers

DevUsers = DevUsers.Where(m=>m.PersonID==1);

gives error "cannot convert source..."

how can i apply where conditions. I need to apply where in condition here for PersonID(int) and Status(int) .

Your DevUsers is an IOrderedEnumerable<T> and can't be implicitly converted to an IEnumerable<T> , so you should define a new variable like this:

var newDevUsers =  DevUsers.Where(m=>m.PersonID==1);

Or modify your last query using AsEnumerable() like this:

//...
}).OrderBy(i => i.FirstName).AsEnumerable();
DevUsers = DevUsers.Where(m=>m.PersonID==1);

Answer to your second question (posted in comment to King King answer) is: m.Tasks is collection ( IOrderedEnumerable<T> in this case) and doesn't have Status property.

If you want all tasks to have given status you should use DevUsers = DevUsers.Where(m => m.Tasks.All(t => t.Status==1)) .
If you want any task to have given status you should use DevUsers = DevUsers.Where(m => m.Tasks.Any(t => t.Status==1)) .

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