mejobloggs
mejobloggs
CC#
Created by mejobloggs on 11/23/2023 in #help
EF/Linq select all columns of main table, but only some cols of joined table?
I find myself often wanting to select all of a table, but very limited columns of joined tables. Is it possible to do without explicitly listing all columns? Example:
public class GetStuff {
public ThingOne GetThingOne() {
var data = new List<ThingOne>() { /*...*/ };

var thing = data.Select(x => new ThingOne {

// I want all ThingOne properties. Can I select them all without having to explicitly mention each one? E.g like a "select ThingOne.*"
A = x.A,
B = x.B,
C = x.C,
D = x.D,
Etc = x.Etc,
MuchMore = x.MuchMore,

//I only want 1 property in ThingTwo
ThingTwo = new ThingTwo {
A2 = x.ThingTwo.A2
}
}).FirstOrDefault();

return thing;
}
}

public class ThingOne {
public ThingTwo ThingTwo { get; set; }
public int A { get; set; }
public int B { get; set; }
public int C { get; set; }
public int D { get; set; }
public int Etc { get; set; }
public int MuchMore { get; set; }
}

public class ThingTwo {
public int A2 { get; set; }
public int B2 { get; set; }
public int C2 { get; set; }
public int D2 { get; set; }
public int Etc2 { get; set; }
}
public class GetStuff {
public ThingOne GetThingOne() {
var data = new List<ThingOne>() { /*...*/ };

var thing = data.Select(x => new ThingOne {

// I want all ThingOne properties. Can I select them all without having to explicitly mention each one? E.g like a "select ThingOne.*"
A = x.A,
B = x.B,
C = x.C,
D = x.D,
Etc = x.Etc,
MuchMore = x.MuchMore,

//I only want 1 property in ThingTwo
ThingTwo = new ThingTwo {
A2 = x.ThingTwo.A2
}
}).FirstOrDefault();

return thing;
}
}

public class ThingOne {
public ThingTwo ThingTwo { get; set; }
public int A { get; set; }
public int B { get; set; }
public int C { get; set; }
public int D { get; set; }
public int Etc { get; set; }
public int MuchMore { get; set; }
}

public class ThingTwo {
public int A2 { get; set; }
public int B2 { get; set; }
public int C2 { get; set; }
public int D2 { get; set; }
public int Etc2 { get; set; }
}
56 replies
CC#
Created by mejobloggs on 9/1/2023 in #help
✅ Easier way to "mark" a class as a different "type" without having to create a new class?
Sorry about the title, I'm not sure of the correct terminology Here's my scenario I have a basic class
public class ValidationResult
{
public ValidationResult(bool isValid, string? errorMessage = null)
{
IsValid = isValid;
ErrorMessage = errorMessage;
}

public bool IsValid { get; set; }
public string? ErrorMessage { get; set; }
}
public class ValidationResult
{
public ValidationResult(bool isValid, string? errorMessage = null)
{
IsValid = isValid;
ErrorMessage = errorMessage;
}

public bool IsValid { get; set; }
public string? ErrorMessage { get; set; }
}
Which is fine, I can display the reason for failure to the user. But I notice when trying to create some unit tests, I'm trying to test for specific failures, and the test might pass because of a different failure... which isn't a proper test then. The test could say Assert.IsTrue(validation.ErrorMessage == "Failure because Reason 1") But then the test would break if I updated the error message. I could have an IValidationResult, then have e.g 10 different classes all exactly the same, inheriting from IValidationResult Then my test could say (pseudocode) Assert.IsTrue(validation is Reason1ValidationResult) That would be less fragile which is good, but then I have to create a bunch of identical classes (except for the name) Is there a better/simpler way to do this?
7 replies
CC#
Created by mejobloggs on 5/24/2023 in #help
❔ Is it a problem to be creating lots of empty lists etc just to avoid null checks?
Over thousands of items, is this expensive or a problem? What is a better way to avoid null checks?
public class MyLists
{
public List<ThingOne> ListOne { get; set; } = new List<ThingOne>();

public List<ThingTwo> ListTwo { get; set; } = new List<ThingTwo>();

public List<ThingThree> ListThree { get; set; } = new List<ThingThree>();

public List<ThingFour> ListFour { get; set; } = new List<ThingFour>();

public List<ThingFive> ListFive { get; set; } = new List<ThingFive>();
}

for(int i = 0; i < 1000000; i++){

MyLists lists = LoadListData(i) ?? new MyLists();

//If no listdata, make a new MyLists so don't have to null check all the time
//MyLists also defaults each list to a new List to avoid null checks

//Is it a problem to be creating thousands of empty lists just to avoid null checks? Is there a better way?


//do some linq on lists
lists.ListOne.Where(...)
lists.ListTwo.FirstOrDefault(...)
lists.ListThree.Select(...)
//etc etc
}
public class MyLists
{
public List<ThingOne> ListOne { get; set; } = new List<ThingOne>();

public List<ThingTwo> ListTwo { get; set; } = new List<ThingTwo>();

public List<ThingThree> ListThree { get; set; } = new List<ThingThree>();

public List<ThingFour> ListFour { get; set; } = new List<ThingFour>();

public List<ThingFive> ListFive { get; set; } = new List<ThingFive>();
}

for(int i = 0; i < 1000000; i++){

MyLists lists = LoadListData(i) ?? new MyLists();

//If no listdata, make a new MyLists so don't have to null check all the time
//MyLists also defaults each list to a new List to avoid null checks

//Is it a problem to be creating thousands of empty lists just to avoid null checks? Is there a better way?


//do some linq on lists
lists.ListOne.Where(...)
lists.ListTwo.FirstOrDefault(...)
lists.ListThree.Select(...)
//etc etc
}
11 replies