Click here to Skip to main content
14,448,095 members
Rate this:
Please Sign up or sign in to vote.
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 2: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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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:
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.
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100