How to create a apex test class for my apex class - testing

It is my first apex class and i don't really know how to implement a proper test class.
My goal is to achieve test coverage of 75%.
I updated based on the comments but i managed to achieve only 70 %. I don't have other idea how to improve this more.
Here is what i did :
Apex class:
public with sharing class AccountController {
#AuraEnabled
public static List<Account> findAll() {
User userDetails =[SELECT Id, Name, Email, Profile.Name, UserRole.Name FROM User
where Id=:userinfo.getUserId() ];
// Theme4t is theme that is used by mobille app for android or iphone
if(((userDetails.UserRole.Name).equals('yon')|| (userDetails.UserRole.Name).equals('bon')|| (userDetails.UserRole.Name).contains('non')
|| (userDetails.UserRole.Name).contains('go')) && UserInfo.getUiTheme() != 'Theme4t'){
return [SELECT id, name, AccountStatus__c, ShippingLatitude, ShippingLongitude, ShippingCity
FROM Account
WHERE ShippingLatitude != NULL AND ShippingLongitude != NULL
LIMIT:22000];
}else {
return [SELECT id, name, AccountStatus__c, ShippingLatitude, ShippingLongitude, ShippingCity
FROM Account
WHERE OwnerId =: UserInfo.getUserId() AND ShippingLatitude != NULL AND ShippingLongitude != NULL
LIMIT:5000];
}
}
Apex test class:
#isTest
public class AccountControllerTest
{
static testMethod void testMethod1()
{
Account acc = new Account();
acc.Name='Test';
insert acc;
User userDetails =[SELECT Id, Name, Email, Profile.Name, UserRole.Name FROM User
where Id=:userinfo.getUserId() ];
List<Account> lstAcc = AccountController.findAll();
UserRole ur =new UserRole();
userDetails.UserRoleId=[select Id from UserRole where Name='yon'].Id;
System.runAs(userDetails){
List<Account> lstAcc1 = AccountController.findAll();
}
userDetails.UserRoleId=[select Id from UserRole where Name='bon'].Id;
System.runAs(userDetails){
List<Account> lstAcc2 = AccountController.findAll();
}
userDetails.UserRoleId=[select Id from UserRole where Name='non'].Id;
System.runAs(userDetails){
List<Account> lstAcc3 = AccountController.findAll();
}
userDetails.UserRoleId=[select Id from UserRole where Name='go'].Id;
System.runAs(userDetails){
List<Account> lstAcc4 = AccountController.findAll();
}
}

Please complete the below trailhead to learn the unit test in Salesforce.
https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro
And also as you are trying to create a user after account insertion it will throw Mixed DML error. you need to use system.runAs() method. follow the below URL for using the method.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_runas.htm
Let me know if still, you need any help on this.
Here is the code for your class and test class. Please follow the best practices from http://blog.shivanathd.com/2013/11/Best-Practices-Test-Class-in-Salesforce.html
This time I am providing the code to you to understand how to create a test class, but next time onwards please follow the steps and documents I have shared.
public with sharing class AccountController {
//using a test visible variable for setting the ui theme check.
#TestVisible static Boolean isTheme4t = UserInfo.getUiThemeDisplayed() == 'Theme4t';
#AuraEnabled
public static List<Account> findAll() {
User userDetails =[SELECT Id, Name, Email, Profile.Name, UserRole.Name FROM User where Id=:userinfo.getUserId()];
// Theme4t is theme that is used by mobille app for android or iphone
if(((userDetails.UserRole.Name).equals('yon')|| (userDetails.UserRole.Name).equals('bon')|| (userDetails.UserRole.Name).contains('non') || (userDetails.UserRole.Name).contains('go')) && !isTheme4t){
return [SELECT id, name, AccountStatus__c, ShippingLatitude, ShippingLongitude, ShippingCity FROM Account WHERE ShippingLatitude != NULL AND ShippingLongitude != NULL LIMIT 22000];
}else {
return [SELECT id, name, AccountStatus__c, ShippingLatitude, ShippingLongitude, ShippingCity FROM Account WHERE OwnerId =: UserInfo.getUserId() AND ShippingLatitude != NULL AND ShippingLongitude != NULL LIMIT 5000];
}
}
}
#isTest
public class AccountControllerTest
{
//Use setup data method to create data and query it in testmethod
#testSetup static void setup() {
UserRole r = new UserRole(DeveloperName = 'yon', Name = 'yon');
insert r;
User u = new User(
ProfileId = [SELECT Id FROM Profile WHERE Name = 'System Administrator'].Id,
LastName = 'last',
Email = 'puser000#amamama.com',
Username = 'puser000#amamama.com' + System.currentTimeMillis(),
CompanyName = 'TEST',
Title = 'title',
Alias = 'alias',
TimeZoneSidKey = 'America/Los_Angeles',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US',
LocaleSidKey = 'en_US',
UserRoleId = r.Id
);
insert u;
System.runAs(u){
Account acc = new Account();
acc.Name = 'Test Account';
acc.ShippingLatitude = 75.46;
acc.ShippingLongitude = 45.46;
acc.AccountStatus__c = 'test';
insert acc;
}
}
static testMethod void testMethod1(){
user u = [select Id from User where email = 'puser000#amamama.com' limit 1];
system.runAs(u){
Test.startTest();
List<Account> acc = [select Id,AccountStatus__c,ShippingLatitude,ShippingLongitude from Account where Name = 'Test Account'];
List<Account> lstAcc4 = AccountController.findAll();
system.assert(lstAcc4.size()>0);
Test.stopTest();
}
}
static testMethod void testMethod2(){
user u = [select Id from User where email = 'puser000#amamama.com' limit 1];
system.runAs(u){
AccountController.isTheme4t = true;
Test.startTest();
List<Account> acc = [select Id,AccountStatus__c,ShippingLatitude,ShippingLongitude from Account where Name = 'Test Account'];
List<Account> lstAcc4 = AccountController.findAll();
system.assert(lstAcc4.size()>0);
Test.stopTest();
}
}
}

Related

Spring boot and hibernate : update all column's rows where entry is null

I've created a query who select all users where a column is null :
#Query("SELECT user from User user where user.maths is null")
List <User> findByMathWhereNull();
Now I would like to calculate some points for each of them with a simple method :
public int calculate(User user){
int maths = calculateByResults(user);
maths += calculateByScore(user.getId());
maths += calculateByBonus(user.getBonus());
return maths;
}
Now, I would like to set the maths variable for each user:
public void saveMaths() {
List<User> users = userRepository.findByMathWhereNull();
// TODO
// for (maths : user) {do something}
how can I put each maths result in the correct column ?
EDIT - SOLUTION:
public void saveIndiceFiabilite() {
List<User> users = userRepository.findByMathWhereNull();
for (User user : users) {
int math = calculateMaths(user);
user.setMahs(maths);
userRepository.updateUser(maths, user.getId());
}
}
U should setMath for each user like this:
public void saveMaths() {
List<User> users = organismeRepository.findByMathWhereNull();
// TODO
for(User user : users){
int math = calculate(user);
user.setMath(math);
}

How to send api request from test class to Main class in salesforce?

Class :
#RestResource(urlMapping='/api/fetch_status/*')
global class RestDemo{
#HttpGet
global static Account getResult(){
Account account;
String accountId = '';
RestRequest restReq = RestContext.request;
RestResponse restRes = RestContext.response;
// reading url
try{
//accountId = restReq.params.get('accountId');
accountId = restReq.requestURI.substring(restReq.requestURI.lastIndexOf('/') + 1);
account = [SELECT Id, Name FROM Account WHERE Id = :accountId Limit 1];
if(account != null){ //checked whether any record is returned or not
restRes.responseBody = Blob.valueOf(JSON.serialize(account));
restRes.statusCode = 200;
}else{
/* String account_not_found= '{"message":"Not Found"}';
restRes.responseBody = Blob.valueOf(account_not_found); */
restRes.statusCode = 404;
}
}
catch(Exception ex){
restRes.responseBody = Blob.valueOf(ex.getMessage());
restRes.statusCode = 500;
}
return account;
}
}
Test Class :
private class RestDemoTest {
#testSetup
static void dataSetup() {
Account acc = new Account(Name = 'Testing5');
insert acc;
}
static testMethod void testGet() {
//case 1 when the id is valid
Account acc = [ SELECT Id FROM Account LIMIT 1 ];
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI = '/services/apexrest/api/fetch_status/' + acc.Id;
req.httpMethod = 'GET';
RestContext.request = req;
RestContext.response= res;
Account acct1 = RestDemo.getResult();
system.assertEquals(acct1.Name, 'Testing5');
// case 2 when the id is not present
String str_id = '0012x000004UjZX';
Id id = Id.valueOf(str_id);
req.requestURI = '/services/apexrest/api/fetch_status/' + id;
req.httpMethod = 'GET';
RestContext.request = req;
RestContext.response= res;
Account acct2 = RestDemo.getResult();
system.assertEquals(acct2, null);
}
}
I want to test the test case 2 where the account with the random id is not present.
But while testing the test class (in test case 2 )it is giving exception.
In this i used an existing id and changed it a little.So while test case2 runs it should go through the else statement of the main class but it is throwing exception (catch statement,Tested it in salesforce developer console).
account = [SELECT Id, Name FROM Account WHERE Id = :accountId Limit 1];
The way you written it account will never be null. It'll just throw "List has no rows for assignment to SObject".
Change to this
List<Account> accs = [SELECT Id, Name FROM Account WHERE Id = :accountId Limit 1];
if(!accs.isEmpty()){
// return 200;
} else {
// return 400;
}
If you query that way it'll always come as List. It can be empty list but it will be a list (not null). Assign query results to single Account/Contact/... only if you're 100% sure it'll return something. Or catch the exceptions

ASP.net Entity Framework Check if exists in database

I have VS2015, entity framework 6. I have a database with one table (Logins)
FirstName, lastName, Birthdate, email, password
I also have a textbox(s), button
tbEmail tbpass and btnLogin
How do I check if the users email in the textbox matches one in the database?
So far I have:
protected void btnLogin_Click(object sender, EventArgs e)
{
Logins Log = new Logins();
using (LoginDataEntities lg = new LoginDataEntities())
{
string #email = tbUsernameL.Text;
string #password = tbPassL.Text;
var logged = from L in lg.Logins
where L.Username == #email
&& L.Pass == #password
select L.Username;
if (logged != null)
{
lblSuccess.Visible = true;
}
else
{
lblFail.Visible = true;
}
}
}
However, its not working and always enables the success label. How do I fix this?
Try it once with the following snippet:
using (LoginDataEntities lg = new LoginDataEntities())
{
string #email = tbUsernameL.Text;
string #password = tbPassL.Text;
var logged = lg.Logins
.SingleOrDefault(l=> l.Username == #email && l.Pass == #password);
if (logged != null) // update
{
lblSuccess.Visible = true;
}
else
{
lblFail.Visible = true;
}
}
Alternatively, can you also look at the following example again:
http://www.c-sharpcorner.com/uploadfile/b19d5a/custom-user-login-and-registration-page-in-Asp-Net-mvc3-with-razor-and-entity-framework/
Or you refactorisiers the VS template with Individual User Accounts

Apex test method error

we are having an issue with apex test method. we have an http request in my apex method. i have to creates a test method for this. but we are having an issue
Apex code:
public void ReceiveBestAnswer(Id repID){
reply = GlobalFunctions.getReplyCreatorDetails(repID);
System.debug(reply);
String bestReplyUser = reply.CreatedById;
profileUser = [select Username,Email from User where Id = :bestReplyUser];
System.debug('profileUser.Name-->'+profileUser.Username);
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
req.setEndpoint('http://sandbox.v2.badgeville.com/api/berlin/b95839370dca983955e550296450ec03/activities.json');
req.setMethod('GET');
req.setBody('activity[verb]=receive best answer&site=www.grazitti.com&user='+ profileUser.Email );
try {
http.send(req);
System.debug('res-->');
} catch(System.CalloutException e) {
System.debug('Callout error: '+ e);
System.debug(res.toString());
}
}
Test method :
public static testMethod void testReceiveBestAnswer() {
Reply reply = new Reply();
String replyId = reply[0].Id;
// Id questionID = '906P0000000010KIAQ';
// String sql = 'SELECT Id, Title FROM Question';
// Question[] ques = [SELECT Id FROM Question];
// Question quest = ques[0];
BadgeVilleWebServiceCallout bv = new BadgeVilleWebServiceCallout();
bv.badgvilleReceiveBestAnswer(replyId );
}
2nd test method:
public static testmethod void testReceiveBestAnswer1()
{
ID ProfileID = [ Select id from Profile where name = 'Named Marketo Community Portal'].id;
System.debug('ProfileID --->'+ProfileID );
Account A1 = new Account(Name = 'Test Account');
insert A1;
List<Contact> ContactList = new List<Contact>();
Contact C1 = new Contact( AccountID = A1.id, FirstName = 'Test', LastName = 'User',
email = 'test-user#fakeemail.com' );
insert C1;
User u1 = new User( email='test-user#fakeemail.com', contactid = c1.id, profileid = profileid,
UserName='test-user#fakeemail.com', alias='tuser1', CommunityNickName='tuser1', TimeZoneSidKey='America/New_York', LocaleSidKey='en_US', EmailEncodingKey='ISO-8859-1', LanguageLocaleKey='en_US', FirstName = 'Test', LastName = 'User' );
insert u1;
System.debug('u1--->'+u1 );
/* User[] users = [SELECT Id FROM User];
User u1 = users[0];
// Question[] ques = [SELECT Id FROM Question];
//System.debug('ques --->'+ques );
// Question quest = ques[0];
// BadgeVilleWebServiceCallout bv = new BadgeVilleWebServiceCallout();
// bv.badgvilleReceiveBestAnswer(quest.Id);
Id questId ;
Test.startTest();
System.RunAs(u1)
{
Question question = new Question();
question.Title = 'Test';
question.Body = 'Test';
question.CommunityId = '09a50000000PNNr';//GlobalFunctions.communityId;
insert question;
System.debug('question-->'+question);
questId = question.Id;
System.debug('questId --->'+questId );
Reply reply1 = new Reply();
reply1.Body = 'reply no 1 ques 1';
reply1.QuestionId = question.Id;
System.debug('reply1--->'+reply1);
insert reply1;
}
// BadgeVilleWebServiceCallout bv = new BadgeVilleWebServiceCallout();
// bv.badgvilleReceiveBestAnswer(questId);
Test.stopTest();
}
We are trying to implement this with both test methods. but we are unable to do it. can nay body have any idea how to do this or where i am doing wrong??
Salesforce doesn't allow you to Test external WebServices.
Here, you have an idea of how to test WebServices.
And here you have a Salesforce Idea.
I hope this help you.

How can I make a nested projection in a Linq query when using the group by clause?

I'm trying to work with grouped data coming back from SQL.
The method I'm writing is to provide the data for a "Case Status Overview" screen.
It must produce a nested XML document.
Now, I could do it the easy way, but I'm trying to learn whether it's possible to use the linq "group by" statement and then to project the data already nested. (the easy way would be just to pull back the data in a tabular fashion from the database and then for-loop through it forming the Xml document for output)
Here is the data hierarchy:
Every Case has a DebtType and every DebtType has a Client.
Here is the SQL that retrieves the data:
SELECT ClientNames.ClientID ,
ClientNames.ClientCode ,
ClientNames.ClientName ,
DebtTypes.DebtTypeID ,
DebtTypes.DebtTypeShortDesc ,
DebtTypes.DebtTypeLongDesc ,
Cases.CurrentStateCode ,
SUM(1 - CAST(Cases.CaseClosed AS INT)) AS OpenCaseCount ,
SUM(CAST(Cases.CaseClosed AS INT)) AS ClosedCaseCount ,
SUM(CAST(Cases.CaseOnHold AS INT)) AS OnHoldCaseCount ,
SUM(CAST(Cases.CaseReferred AS INT)) AS ReferredCaseCount ,
COUNT(Cases.CaseID) AS TotalCaseCount ,
SUM(Cases.CaseTotalPaid) AS TotalAmountPaid ,
SUM(Cases.CaseCurrentOutstandingAmount) AS TotalAmountOutstanding,
SUM(Cases.CaseTotalDebtWrittenOff) AS TotalAmountWrittenOff ,
SUM(Cases.CaseTotalDebtCancelled) AS TotalAmountCancelled
FROM ClientNames
INNER JOIN ClientDebtTypes
ON ClientNames.ClientID = ClientDebtTypes.ClientID
INNER JOIN DebtTypes
ON ClientDebtTypes.DebtTypeID = DebtTypes.DebtTypeID
INNER JOIN Cases
ON ClientDebtTypes.ClientDebtTypeID = Cases.CaseClientDebtTypeID
GROUP BY ClientNames.ClientID ,
ClientNames.ClientCode ,
ClientNames.ClientName ,
DebtTypes.DebtTypeID ,
DebtTypes.DebtTypeShortDesc,
DebtTypes.DebtTypeLongDesc ,
Cases.CurrentStateCode
ORDER BY ClientNames.ClientID,
DebtTypes.DebtTypeID,
CurrentStateCode
Using Linqer it converts it to:
from clientnames in db.ClientNames
join clientdebttypes in db.ClientDebtTypes on clientnames.ClientID equals clientdebttypes.ClientID
join debttypes in db.DebtTypes on clientdebttypes.DebtTypeID equals debttypes.DebtTypeID
join cases in db.Cases on new { ClientDebtTypeID = clientdebttypes.ClientDebtTypeID } equals new { ClientDebtTypeID = cases.CaseClientDebtTypeID }
group new {clientnames, debttypes, cases} by new {
clientnames.ClientID,
clientnames.ClientCode,
clientnames.ClientName1,
debttypes.DebtTypeID,
debttypes.DebtTypeShortDesc,
debttypes.DebtTypeLongDesc,
cases.CurrentStateCode
} into g
orderby
g.Key.ClientID,
g.Key.DebtTypeID,
g.Key.CurrentStateCode
select new {
ClientID = (System.Int32?)g.Key.ClientID,
g.Key.ClientCode,
g.Key.ClientName1,
DebtTypeID = (System.Int32?)g.Key.DebtTypeID,
g.Key.DebtTypeShortDesc,
g.Key.DebtTypeLongDesc,
g.Key.CurrentStateCode,
OpenCaseCount = (System.Int64?)g.Sum(p => 1 - Convert.ToInt32(p.cases.CaseClosed)),
ClosedCaseCount = (Int32?)g.Sum(p => Convert.ToInt32(p.cases.CaseClosed)),
OnHoldCaseCount = (Int32?)g.Sum(p => Convert.ToInt32(p.cases.CaseOnHold)),
ReferredCaseCount = (Int32?)g.Sum(p => Convert.ToInt32(p.cases.CaseReferred)),
TotalCaseCount = (Int64?)g.Count(p => p.cases.CaseID != null),
TotalAmountPaid = (System.Decimal?)g.Sum(p => p.cases.CaseTotalPaid),
TotalAmountOutstanding = (System.Decimal?)g.Sum(p => p.cases.CaseCurrentOutstandingAmount),
TotalAmountWrittenOff = (System.Decimal?)g.Sum(p => p.cases.CaseTotalDebtWrittenOff),
TotalAmountCancelled = (System.Decimal?)g.Sum(p => p.cases.CaseTotalDebtCancelled)
}
Now as I mentioned, I could stop there and right a for loop to create the Xml data.
But I'm trying to create a nested group (IGrouping<ClientName,IGrouping<DebtType,SummaryClass>>)
and then project the data in a nested format.
Now we're using LinqToXsd to create strong type wrappers for out Xml documents, but essentially all this means is that out output type is:
private class ClientSummary
{
public string ClientName { get; set; }
public IList<DebtTypeSummary> DebtTypes { get; set; }
}
private class DebtTypeSummary
{
public string DebtType { get; set; }
public IList<StateCodeSummary> StateCodes { get; set; }
}
private class StateCodeSummary
{
public string StateCode { get; set; }
public int TotalCount { get; set; }
public decimal TotalAmountPaid { get; set; }
//etc
//etc
//etc
}
Now I got as far as writing the following Linq:
var grouping = from cases in db.Cases
join clientdebttypes in db.ClientDebtTypes on cases.CaseClientDebtTypeID equals clientdebttypes.ClientID
join debttypes in db.DebtTypes on clientdebttypes.DebtTypeID equals debttypes.DebtTypeID
group cases by new ClientDebtTypePair() { ClientDebtType = clientdebttypes, DebtType = debttypes } into casesByClientDebtTypes
join clientnames in db.ClientNames on casesByClientDebtTypes.Key.ClientDebtType.ClientName equals clientnames
group casesByClientDebtTypes by clientnames;
var projected = from casesByClientDebtTypes in grouping
let client = casesByClientDebtTypes.Key
select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType()
{
Client = new Client()
{
ClientID = client.ClientID,
DisplayName = client.ClientName1,
},
DebtTypes = from cases in casesByClientDebtTypes
let debttype = cases.Key.DebtType
select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType.DebtTypesLocalType()
{
DebtType = new DebtType()
{
DebtTypeID = debttype.DebtTypeID,
Description = debttype.DebtTypeLongDesc,
DisplayName = debttype.DebtTypeShortDesc,
},
StatesCodes = from cases2 in cases
select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType.DebtTypesLocalType.StatesCodesLocalType()
{
ClosedCasesCount = cases2.Sum(p => Convert.ToInt32(p.cases.CaseClosed))
which joins and groups the database tables and then tries to project the result a ClientSummary (the class names are different but that's because the above is a simplified view of the output classes). I fail completely when I've drilled all the way down to the Cases table and I find that I don't really understand how to do agregate functions. They appear to only be available on IGrouping<K, T>s and it seems I've just got confused.
I need to also ensure that the summaries are calculated server side, pulling back millions of cases would be bad.
Can anybody help me with this one? Is this even possible?
Regards,
James.
-------### UPDATE 1 ###-------
OK, been working on this again today.
I decided to use Linq2SQL to pull pack 2D data and then reformat it using Linq2Objects.
Here is what I started with:
var sql = from clientnames in db.ClientNames
join clientdebttypes in db.ClientDebtTypes on clientnames.ClientID equals clientdebttypes.ClientID
join debttypes in db.DebtTypes on clientdebttypes.DebtTypeID equals debttypes.DebtTypeID
join cases in db.Cases on new { ClientDebtTypeID = clientdebttypes.ClientDebtTypeID } equals new { ClientDebtTypeID = cases.CaseClientDebtTypeID }
group new { clientnames, debttypes, cases } by new
{
clientnames.ClientID,
clientnames.ClientCode,
clientnames.ClientName1,
debttypes.DebtTypeID,
debttypes.DebtTypeShortDesc,
debttypes.DebtTypeLongDesc,
cases.CurrentStateCode
} into g
orderby
g.Key.ClientID,
g.Key.DebtTypeID,
g.Key.CurrentStateCode
select new
{
Client = new Client{ ClientID = g.Key.ClientID, DisplayName = g.Key.ClientName1 },
DebtType = new DebtType{ DebtTypeID = g.Key.DebtTypeID, DisplayName = g.Key.DebtTypeShortDesc, Description = g.Key.DebtTypeLongDesc },
StateSummary = new LoadCaseStatusOverviewScreenOutput.ClientsLocalType.DebtTypesLocalType.StatesCodesLocalType()
{
StateCode = g.Key.CurrentStateCode,
OpenCasesCount = g.Sum(p => 1 - Convert.ToInt32(p.cases.CaseClosed)),
ClosedCasesCount = g.Sum(p => Convert.ToInt32(p.cases.CaseClosed)),
OnHoldCasesCount = g.Sum(p => Convert.ToInt32(p.cases.CaseOnHold)),
ReferredCasesCount = g.Sum(p => Convert.ToInt32(p.cases.CaseReferred)),
TotalCasesCount = g.Count(p => p.cases.CaseID != null),
TotalAmountPaid = g.Sum(p => p.cases.CaseTotalPaid),
TotalAmountOutstanding = g.Sum(p => p.cases.CaseCurrentOutstandingAmount),
TotalAmountWrittenOff = g.Sum(p => p.cases.CaseTotalDebtWrittenOff),
TotalAmountCancelled = g.Sum(p => p.cases.CaseTotalDebtCancelled),
}
};
var res = sql.ToList();
output.Clients = (from results in res
group results by results.Client into resultsByClient
from resultsByDebtType in
(from results in resultsByClient
group results by results.DebtType)
group resultsByDebtType by resultsByClient.Key into resultsByDebtTypeByClient
select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType()
{
Client = resultsByDebtTypeByClient.Key,
DebtTypes = (from resultsByDebtType in resultsByDebtTypeByClient
select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType.DebtTypesLocalType()
{
DebtType = resultsByDebtType.Key,
StatesCodes = (from results in resultsByDebtType
let summary = results.StateSummary
select results.StateSummary).ToList()
}).ToList()
}).ToList();
That runs, but produces one Client/DebtType/Summary set for every result. So even though there is only one client in this case, I end up with 1300 clients, all identical.
I simplified it to the following:
output.Clients = (from results in res
group results by results.Client into resultsByClient
select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType()
{
Client = resultsByClient.Key,
DebtTypes = null,
}).ToList();
That produces 1300 clients. Next I tried this:
output.Clients = (from results in res
group results by results.Client.ClientID into resultsByClient
select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType()
{
Client = new Client { ClientID = resultsByClient.Key },
DebtTypes = null,
}).ToList();
And THAT produces ONE client (hurray!). Except I loose all the client information (boo!)
Guessing that as it's comparing client by refernce instead of by content I wrote the following:
public partial class Client
{
public static bool operator ==(Client left, Client right)
{
return left.ClientID == right.ClientID;
}
public static bool operator !=(Client left, Client right)
{
return left.ClientID != right.ClientID;
}
public override int GetHashCode()
{
return ClientID;
}
}
That did nothing. It repeatedly calls GetHashCode(), which I fudged to force it to return the same hash code for any matching ClientID, but it still created 1300 Client groups.
Regards,
James.
-------### UPDATE 2 ###-------
OK, I thought I would have a go at making the Linq2Sql output only simple values for grouping by:
g.Key.ClientID,
g.Key.ClientName1,
g.Key.DebtTypeID,
g.Key.DebtTypeShortDesc,
g.Key.DebtTypeLongDesc,
And then changed the test Linq2Objects to:
output.Clients = (from results in res
group results by new { ClientID = results.ClientID, DisplayName = results.ClientName1 } into resultsByClient
select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType()
{
Client = new Client { ClientID = resultsByClient.Key.ClientID, DisplayName = resultsByClient.Key.DisplayName },
DebtTypes = null,
}).ToList();
That works. So anonymous types compare in the way I want them to, by content not reference (apparently)
This does not:
output.Clients = (from results in res
group results by new SiDemClient { ClientID = results.ClientID, DisplayName = results.ClientName1 } into resultsByClient
select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType()
{
Client = resultsByClient.Key,//new Client { ClientID = resultsByClient.Key.ClientID, DisplayName = resultsByClient.Key.DisplayName },
DebtTypes = null,
}).ToList();
That still creates 1300 groups.
So, anonymous types compare in a magical way that I don't understand. How can I make my Client class compare like an anonymous type?
Regards,
James.
-------### SOLUTION FOUND ###-------
-------### MANY THANKS TO Enigmativity ###-------
I needed to override the Equals() method instead of implementing the == operator.
Now the grouping works and I have a wonderful Xml document to reutrn!
public partial class SiDemClient
{
public override bool Equals(object obj)
{
if (obj is SiDemClient)
{
return this.ClientID.Equals(((SiDemClient)obj).ClientID);
}
return false;
}
public override int GetHashCode()
{
return ClientID;
}
}
Many Thanks,
James.
When you override GetHashCode you must also override Equals. The == & != operators are irrelevant.
Try with this:
public partial class Client
{
public override bool Equals(object obj)
{
if (obj is Client)
{
return this.ClientID.Equals(((Client)obj).ClientID);
}
return false;
}
public override int GetHashCode()
{
return this.ClientID.GetHashCode();
}
}
See if that helps.