Click here to Skip to main content
15,889,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i try to group by using entity framework after filtering (Where Clause)

but this error appear :
Cannot implicitly convert type 'System.Linq.IQueryable<dataObjects.CLSPeopleActivities>' to 'System.Linq.IQueryable<view_people_history>'. An explicit conversion exists (are you missing a cast?)


at :
.Select(grp => new CLSPeopleActivities
                {
                    ID = grp.Key.PeopleID,
                    Name = grp.Key.pepole_Name,
                    Count = grp.Count(),
                    Amount = grp.Sum(f => f.NetValue),
                    Amount_Curr = grp.Sum(f => f.NetValue).ToString() + " " + currency,
                    PercentOfTotal_Count = (grp.Count() / sTotalCount) * 100,
                    PercentOfTotal_Amount = (grp.Sum(f => f.NetValue) / sTotalAmount) * 100,
                    PercentOfTotal_Count_perc  = ((grp.Count() / sTotalCount) * 100).ToString() + "%",
                    PercentOfTotal_Amount_perc = ((grp.Sum(f => f.NetValue) / sTotalAmount) * 100).ToString() + "%",
                });


What I have tried:

var Query = DB1.view_people_history.Where(u=> u.PeopleID != 0);

            // Filter With Date
            if (checkDate.Checked)
            {
                Query = Query.Where(u => u.Opdate >= From && u.Opdate <= To);
            }
            
            // Branchs
            if (checkBranch.Checked && branchids.Count() > 0) { Query = Query.Where(u => branchids.Contains(u.BranchID)); }

            // Exclude the main account
            if (checkOperation.Checked) { Query = Query.Where(u => !PeopleMainAccounts.Contains(u.PeopleID)); }

            // Filter With Operation Type
            switch (OpTypeInput.EditValue)
            {
                case "purchases":
                    Query = Query.Where(u => u.OpType == "PURCHASES");
                    break;

                case "sales":
                    Query = Query.Where(u => u.OpType == "SALES");
                    break;

                case "rpurchases":
                    Query = Query.Where(u => u.OpType == "RETURNED_PURCHASES");
                    break;

                case "rsales":
                    Query = Query.Where(u => u.OpType == "RETURNED_SALES");
                    break;
            }

            // get Totals
            int sTotalCount = Query.Count();
            decimal sTotalAmount = Query.Sum(s => s.NetValue.Value);
            string currency = CurrencyInfo.currencyShortcut();
            // Group
            Query = Query.GroupBy(s => new  
                {
                    s.PeopleID,
                    s.pepole_Name
                })
                .Select(grp => new CLSPeopleActivities
                {
                    ID = grp.Key.PeopleID,
                    Name = grp.Key.pepole_Name,
                    Count = grp.Count(),
                    Amount = grp.Sum(f => f.NetValue),
                    Amount_Curr = grp.Sum(f => f.NetValue).ToString() + " " + currency,
                    PercentOfTotal_Count = (grp.Count() / sTotalCount) * 100,
                    PercentOfTotal_Amount = (grp.Sum(f => f.NetValue) / sTotalAmount) * 100,
                    PercentOfTotal_Count_perc  = ((grp.Count() / sTotalCount) * 100).ToString() + "%",
                    PercentOfTotal_Amount_perc = ((grp.Sum(f => f.NetValue) / sTotalAmount) * 100).ToString() + "%",
                });
Posted
Updated 8-Jun-19 1:43am
v2
Comments
Richard MacCutchan 8-Jun-19 6:37am    
Do what the message tells you, and use a cast to the correct type.

1 solution

All it's saying is that the two types are related, but that no implicit conversion exists so it won't let you "just assign" the value - you can explicitly cast it because an explicit conversion does exist, but you have to make the compiler understand that that is what you wanted to do.

Because you declared Query as a var it gets it's type by context:
C#
var Query = DB1.view_people_history.Where(u=> u.PeopleID != 0);
And that makes it an IQueryable<view_people_history>Your subsequent Linq code returns a related class: IQueryable<dataObjects.CLSPeopleActivities> but that isn't exactly the same type so the system wants to make sure that is what you intended.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900