|
I have this query:
var currentYearData = (from p in db.Projects
join co in db.Companies on p.CompanyId equals co.Id
join cty in db.Cities on p.CityId equals cty.Id
join j in db.Jobs on p.Id equals j.ProjectId
join jsdr in db.JobStartDateRevisions on j.Id equals jsdr.JobId
where jsdr.StartDate >= start && jsdr.StartDate <= end
select new { p, c=co, cty, j, jsdr })
.OrderBy(x => x.c.CompanyName)
.ThenBy(x => x.p.ProjectName)
.ThenBy(x => x.jsdr.StartDate)
.ToList();
Where I join on JobStartDateRevisions, I only want to join to the NEWEST JobStartDateRevision record.
Is this possible? Can someone show me how to do this?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
You don't join on a condition you need to filter the set to get what you want. You could try by ordering the set by StartDate descending and then take 1 record from the top of the set.
"I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP
|
|
|
|
|
While Nullable Reference Types are great, in some ways they just kick the problem down the road. In a workflow an object may allow a field to be null in an early step, but mandatory in a later step. I call these Temporal Nulls . If we want our objects to accurately reflect the business case/state NRT's introduce a new design decision. How are you approaching this?
Explainer
In Step 1 I am gathering data. Assume I am not creating the object here, just adding Info data.
In Step 2 I am using the data. At this point Info is required data.
The object is the same object from a business perspective, but from a technology perspective it has two states.
class MyInfo {
public InfoData? Info;
}
class MyInfo {
public InfoData Info;
}
I can think of three general approaches to this.
1. Use multiple classes
2. Use properties or multiple fields
3. Use defaults
The multiple class approach (above) is the most "accurate" but multiplies maintenance and would require different names for the classes (e.g., MyInfo & MyInfoWip).
A property or multiple field approach can look something like this
class MyInfo {
private _infoData?;
public InfoData {
get => _infoData ?? throw ...
set => _infoData = value
}
}
The drawback here is that I've basically exchange a null for an error and I don't know until I try to use it.
The defaults approach is getting common using Empty
class MyInfo {
public InfoData = InfoData.Empty;
public static InfoData Empty => new InfoData();
}
This is just kicking the can. I've replaced a null check with a "Empty Check". It requires defining what Empty values required fields get - new InfoData(emptyValues) .
What other approaches come to mind, and how do you normally deal with Temporal Nulls?
|
|
|
|
|
private string _info = null;
public string Info { get { return _info ?? "I'm empty"; } set { _info = value; } }
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
|
I suggest you look at the new static analysis features/attributes in C# 8: [^]
Quote: AllowNull: A non-nullable input argument may be null.
DisallowNull: A nullable input argument should never be null.
MaybeNull: A non-nullable return value may be null.
NotNull: A nullable return value will never be null.
MaybeNullWhen: A non-nullable input argument may be null when the method returns the specified bool value.
NotNullWhen: A nullable input argument will not be null when the method returns the specified bool value.
NotNullIfNotNull: A return value isn't null if the argument for the specified parameter isn't null.
DoesNotReturn: A method never returns. In other words, it always throws an exception.
DoesNotReturnIf: This method never returns if the associated bool parameter has the specified value. I recommend Jonathan Allen's coverage of these features on InfoQ: >[^]
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
modified 28-Oct-20 5:06am.
|
|
|
|
|
Hi guys
I need your help with developing this program...
Using C# Console application, design a program that uses nested for
loop statements to display students and their respective locations in a string matrix.
|
|
|
|
|
What have you tried? Where are you stuck? What does your code look like right now?
|
|
|
|
|
I didn't start yet because I have no idea to go on with it because the question is asking to design a program that uses nested for
loop statements to display students and their respective locations in a string matrix, and I have never come across such a question.
|
|
|
|
|
Your instructor must have covered the necessary points already, right?
Were you not paying attention?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Quote: Go to ParentI didn't start yet because I have no idea to go on with it because the question is asking to design a program that uses nested for
loop statements to display students and their respective locations in a string matrix, and I have never come across such a question.
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.
So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^] and then read your recent course notes again. When you understand them, read the whole question he set carefully, and think about what he is trying to get you to do. This isn't a complicated exercise - it's very basic stuff he needs you to learn here - so a little thought should get it working.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi, folks.
I’m developing an application in C# using Visual Studio. This application has a parent form from which the user can call other forms. My original problem was: if the user has already launched a form and he clicks again in the menu item to launch the same form, the program should only focus on the form. If the form is not on screen, the program should launch it. Another problem is that I have 1 form that is launched with 2 different data sets (different tables in a DB).
I solved this problem this way: I declared 2 variables for the forms and defined 2 routines:
private void M2_SaidaEfetiva_Click(object sender, EventArgs e)
{
if (FormEfetivas == null)
{
FormEfetivas = new Frm_Saida();
FormEfetivas.MdiParent = this;
FormEfetivas.Text = "Saídas Efetivas";
FormEfetivas.Tag = "1";
FormEfetivas.Show();
}
FormEfetivas.Activate();
}
private void M2_SaidaPrevisao_Click(object sender, EventArgs e)
{
if (FormPrevisao == null)
{
FormPrevisao = new Frm_Saida();
FormPrevisao.MdiParent = this;
FormPrevisao.Text = "Previsão de Saídas";
FormPrevisao.Tag = "0";
FormPrevisao.Show();
} Now the problem is: when the user opens one form, then closes it, he cannot reopen it again, because the variables (FormEfetivas and FormPrevisao) are not eliminated when the user closes the form.
What am I forgetting here? Can anybody help me?
Thanks.
|
|
|
|
|
Add a handler to the Form.Closed event when you create the instance, and in the handler set the appropriate variable (for example FormEfetivas to null. The existing code will then generate a new instance when they try next time.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi, OriginalGriff.
I didn't follow you. Could you explain better? Or give me an example?
Thanks.
|
|
|
|
|
What part doesn't make sense?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
It's not a matter of making sense. I understand what you wrote, but I'm confused about how to do it.
Could you explain it?
Thanks.
|
|
|
|
|
Explain what? I have no idea which bits you are capable of doing for yourself!
So ... you know how to add an event handler, yes?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
Simple enough:
private void M2_SaidaEfetiva_Click(object sender, EventArgs e)
{
var theForm = Application.OpenForms.OfType<Frm_Saida>().FirstOrDefault();
if (theForm is null)
{
theForm = new Frm_Saida();
theForm.MdiParent = this;
theForm.Text = "Saídas Efetivas";
theForm.Tag = "1";
theForm.Show();
}
theForm.Activate();
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi all,
I would appreciate a hand in designing my code.
I have a number of classes for different entities (currently 7 but there can be more in the future). I need a method in each of these classes that authorizes user's access to a file resource. Implementation varies but an exception must be thrown if user does not have access to the file in question.
Problem is, when the file is being accessed, I need to call the authorization method of the correct class based on the data on the file. Files are stored in a db table and one of the fields in that table determines which entity the file is related to and how to find which user has access to the file.
So I must read the file data from db, then see which entity it's related to and then call the authorization method of the correct class.
Is there a way in C# to accomplish this without having to make a switch case structure on the file field value and initialize a class and call a method?
Example of the classes used:
public class FileAttachment
{
protected void AuthorizeFileAccess(FileAttachmentData file)
{
}
}
public class ChatMessage
{
public int SenderUserId;
public int RecipientUserId;
}
|
|
|
|
|
Your words: "read the file data from the db"; i.e. no switch. The "file data" would identify the "class type" to be instantiated for your "method call".
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
I am baffled by how the code in the 'Human and 'Cat classes can successfully call a generic static method (SetCache<T>(T value) ) without supplying the usual <Type> adornment.
Here's a sample usage of the code:
BeingCache cache = BeingCache.CacheInstance;
Human ahuman = new Human("joe", "male");
Cat acat = new Cat("Meow", true);
IBeing b1 = cache[1];
IBeing<Cat> b2 = (IBeing<Cat>) b1;
Cat cat = b2.IBInstance;
bool st0 = b2 is Cat;
bool st1 = b2 is Being<Cat>;
bool st2 = b2 is IBeing<Cat>;
Cat hepcat = BeingCache.GetBeing<Cat>(cache[1]);
IBeing<Cat> b3 = (IBeing<Cat>) cache[1]; The example: note that classes 'Human and 'Cat do not inherit from anything.
using System;
using System.Collections.Generic;
namespace InterfaceInheritance_2020
{
public class Human
{
public Human(string name, string gender)
{
Name = name;
Gender = gender;
BeingCache.SetCache(this);
}
public string Gender{ get;}
public string Name { get; }
}
public class Cat
{
public Cat(string name, bool haswhiskers = true)
{
Name = name;
HasWhiskers = haswhiskers;
BeingCache.SetCache(this);
}
public bool HasWhiskers { get; }
public string Name { get; }
}
public interface IBeing
{
Type BeingType { set; get; }
}
public interface IBeing<T> : IBeing
{
T IBInstance { set; get; }
}
public class Being<T> : IBeing<T>
{
public Being(T content)
{
IBInstance = content;
BeingType = typeof(IBeing<T>);
}
public Type BeingType { get; set; }
public T IBInstance { get; set; }
}
public sealed class BeingCache : List<IBeing>
{
private BeingCache()
{
}
public static BeingCache CacheInstance{ get; } = new BeingCache();
public static void SetCache<T>(T value)
{
CacheInstance.AddBeing(new Being<T>(value), typeof(T));
}
public void AddBeing(IBeing being, Type btype)
{
being.BeingType = btype;
CacheInstance.Add(being);
}
public static T GetBeing<T>(IBeing being)
{
return (T) ((IBeing<T>) being).IBInstance;
}
}
}
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
The type is inferred because you pass in a value of the type in your call
public static void SetCache<T>(T value)
"I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP
|
|
|
|
|
Hi, Appreciate your response. I am still baffled: when BeingCache.SetCache is called within the two non-generic classes 'Human and 'Cat ... like this:
BeingCache.SetCache(this); // :wtf:
How does the generic static method:
public static void SetCache<T>(T value) ''
not throw a compile time error, or a run-time error ?
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
I had a similar conversation with jsop on here who wrote an excellent article on using reflection in a DAL - he had the method below in one of his classes
public static T ConvertFromDBValue<T>(object obj, T defaultValue)
{
T result = (obj == null || obj == DBNull.Value) ? default(T) : (T)obj;
return result;
}
and he made calls like this
object propvalue;
Type proptype;
propvalue = ConvertFromDBValue(propvalue, proptype.GetDefaultValue());
It just works - however if you change the parameter T defaultValue to a type other than T it won't compile unless you provide the type in <>.
"I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP
|
|
|
|