简体   繁体   中英

C# Linq Assign Anonymous Type in GroupBy

My sql database does not include fault code owners, those details are stored within an xml file Is it possible to Groupby FaultCodeOwner when that data is coming from an external source?

I get the following error: cannot assign void to anonymous type property

        var query = referenceDt.AsEnumerable()
            .Where(results => declarations.CaapcityIssues.Contains((results.Field<string>("FabricName"))))
            .GroupBy(results => new
            {
                **FaultCodeOwner = faultCodeDetails.getFacultCodeOwner(results.Field<int>("FaultCode"), out owner)**
            })
            .OrderBy(newFaultCodes => newFaultCodes.Key.FaultCodeOnwer)
            .Select(newFaultCodes => new
            {
                FaultCodeOwner = newFaultCodes.Key.FaultCodeOwner,
                Count = newFaultCodes.Count()
            });

You cannot group by anything that is not in the database without bringing your query results in memory first.

Inserting ToEnumerable or ToList after the Where method will do that. Unfortunately, you may bring more data in memory than you otherwise would.

Change the GroupBy method to this:

.GroupBy(results =>
{
    FaultCodeOwnerType faultCodeOwner; // rename FaultCodeOwnerType to the type of FaultCodeOwner
    faultCodeDetails.getFacultCodeOwner(results.Field<int>("FaultCode"), out faultCodeOwner);
    return new
    {
        FaultCodeOwner = faultCodeOwner
    };
})

faultCodeDetails.getFacultCodeOwner returns void , so you can't assign a variable to it. You have to first declare a variable of the FaultCodeOwner's type then pass it as an out parameter to getFacultCodeOwner , which will assign it for you.

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