Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 days ago.
Improve this question
I'm using a dictionary in C# and want to make the value a custom class. I have the following code.
public class myMovies
{
public string Name { get; set; }
public string Year { get; set; }
}
Dictionary<string, myMovies> file_dict = new Dictionary<string, myMovies>();
foreach (string file in Directory.GetFiles(path1, "*.mkv", SearchOption.AllDirectories))
{
file_dict.Add(file, new myMovies("x", "x");
}
I'm doing something wrong, I just have no idea at this point. As a side note, this code does work when I just use a <string,string> dictionary and just store a text value in the value pair.
Edit
Required a constructor in my class definition. Thanks for help.
Either provide an appropriate constructor in the class definition:
public class myMovies
{
public string Name { get; set; }
public string Year { get; set; }
public myMovies(string name, string year)
{
Name = name;
Year = year;
}
}
Or use object initializer syntax to assign the property values when instantiating the object:
file_dict.Add(file, new myMovies { Name = "x", Year = "x" });
It's telling you it expects a Constructor, so give it what it expects:
public class myMovies
{
public myMovies(string name, string year)
{
Name = name;
Year = year;
}
public string Name { get; set; }
public string Year { get; set; }
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
How could I instantiate the Flights in one line?
public class Flights
{
public RouteInformation routeInformation { get; set; }
public string FlightNumber { get; set; }
}
public class RouteInformation
{
public string Origin { get; set; }
public string Destination { get; set; }
}
I want to instantiate the Flights in one line
var flights = new Flights {
new RouteInformation
{
Origin="IKA",
Destination="IST"
},
FlightNumber="123",
};
But I got Invalid initializer member declarator.
var flights = new Flights {
routeInformation = new RouteInformation
{
Origin="IKA",
Destination="IST"
},
FlightNumber="123",
};
You forgot "routeInformation ="
My application is basically just a survey with questions and multiple choice answers. Questions have Answers, but a specific Answer may lead to a specific Question being asked which might otherwise not be asked at all. e.g. "Do you like chocolate?" (if yes ask...) "Do you prefer German or Dutch chocolate?"
In the Answer class, I am trying to populate a list property "DependentQuestions" which is meant to contain id numbers for the Quesiton(s) that will be asked next if this Answer is chosen. The problem is, I am always getting nothing and I'm not sure why. I confirmed the Answer.id is zero at the time the constructor runs by populating DependentQuestions with the commented code you'll see below.
Each Question has an icollection of Answers.
Question class:
public class Question
{
[Key]
public int id { get; set; }
public string question { get; set; }
public int? DependentAnswer { get; set; }
public virtual ICollection<Answer> answers { get; set; }
}
Answer class:
public class Answer
{
[Key]
public int id { get; set; }
public string answer { get; set; }
[Required]
public int questionId { get; set; }
public List<int> DependentQuestions { get; set; }
public Answer()
{
DependentQuestions = new List<int>();
using (dbSurvey db = new dbSurvey())
{
var _list = db.Questions.Where(q => q.DependentAnswer == id).Select(q => q.id).ToList();
if (_list.Any())
{
DependentQuestions.AddRange(_list);
}
//else
//{
// DependentQuestions.Add(id);
//}
}
}
}
The "answers" collection of the Question class is being filled with the Answers to the given Question and that works just fine, but the DependentQuestions list in the Answer class is always coming up empty since Answer.id is always zero at that point. So why is Answer.id always 0, and what can I do about it?
Constructor code is run before any property values are set, so at the point of executing the constructor all properties just contain their default values. That is why it is always 0.
I am not sure what are you using as a data access framework, but generally you can do few things:
Create Answer entity with id, so you always have it in constructor:
public Answer(int id)
If that's not an option, you could also have a lazy property loading questions as needed:
class Answer
{
private List<int> _dependentQuestions;
public List<int> DependentQuestions
{
get
{
if (_dependentQuestions == null)
// load questions here
return _dependentQuestions;
}
}
}
Note that this assumes id is already set, you probably should validate that too.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
In my model Account I have a property like this
public List<String> Roles { get; set; }
Later on I want to get that property but convert it IList<IApplicationUserRole<Role>>, so I have this function
public IList<IApplicationUserRole<Role>> Roles
{
get
{
return _account.Roles; // how do I convert this in the specific type intended.
}
}
Here is my IApplicationUserRole
public interface IApplicationUserRole<TRoleModel> : IRole<string>
where TRoleModel : EntityModel
{
TRoleModel Role { get; set; }
String Name { get; set; }
}
I am a newbie to this thing. Looking forward for any help.
Say you have your implementing class be something like:
public class ApplicationUserRole : IApplicationUserRole<T> where T : Role
{
public ApplicationUserRole()
{
}
public User User { get; set; }
public T Role { get; set; }
}
Then, you'd do something like this:
public IList<IApplicationUserRole<Role>> Roles
{
get
{
return _account.Roles
.Select(r => new ApplicationUserRole { Role = roleService.FindRoleByName(r) })
.Cast<IApplicationUserRole<Role>>()
.ToList();
}
}
Where roleService is some way of building a Role instance from the role name (which above is r)
NOTE: This being said, there is a catch in the above implementation. Since Roles is a property it should not do data access operations. So, in this case, you should create a method instead of a property.
I would start with something like this:
public IList<IApplicationUserRole<Role>> Roles
{
get
{
return _account.Roles.Select(r=>
new ApplicationUserRole<Role>() {Name = r})
.Cast<IApplicationUserRole<Role>>()
.ToList();
}
}
This assuming that you have a class that implements the IApplicationUserRole<Role> interface.
As #MartinLiversage says you can't directly convert List<T> to List<U>, you have to manually do the conversion.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have the following XML
<Workflow Name="Workflow1">
<Parameter Name="Parameter1">Value1</Parameter>
<Parameter Name="Parameter2">Value2</Parameter>
<Environment Name="Environment1" Type="Typ1">
<DataCenter Name="DC1" DeployEnvironmentName="blah"/>
</Environment>
<Environment Name="Environment2" Type="Typ2">
<DataCenter Name="DC2" DeployEnvironmentName="blah"/>
</Environment>
I am trying to read this XML into the following objects
class Workflow
{
public string Name { get; set; }
public IEnumerable<Parameter> Parameters { get; set; }
public IEnumerable<Environment> Environments { get; set; }
}
class Environment
{
public string Name { get; set; }
public EnvironmentType Type { get; set; }
public IEnumerable<DataCenter> DataCenters { get; set; }
}
class Parameter
{
public string Name { get; set; }
public string Value { get; set; }
}
class DataCenter
{
public string Name { get; set; }
public string DeployEnvironmentName { get; set; }
}
using the following expression
var root = XElement.Load(filePath);
var workflows =
root.Elements("Workflow")
.Select(
e =>
new Workflow
{
Name = e.Attribute("Name").Value,
Parameters = e.Elements("Parameter")
.Select(p =>
new Parameter { Name = p.Attribute("Name").Value, Value = p.Value }),
Environments = e.Elements("Environment").Select(
p =>
new Environment
{
Name = p.Attribute("Name").Value,
Type = (EnvironmentType)Enum.Parse(typeof
(EnvironmentType), p.Attribute("Type").Value, true),
DataCenters = p.Elements("DataCenter").Select(
dc => new DataCenter {
Name = dc.Attribute("Name").Value, DeployEnvironmentName = dc.Attribute
("DeployEnvironmentName").Value })
});
});
I keep getting a syntax error. For some reason, it doesn't seem to like these nested expressions. Anyone know what could be going wrong or know of a better way to do this? Thanks in advance
you need to remove this semicolon
Name = p.Attribute("Name").Value,
Type = (EnvironmentType)Enum.Parse(typeof
(EnvironmentType), p.Attribute("Type").Value, true),
DataCenters = p.Elements("DataCenter").Select(
dc => new DataCenter {
Name = dc.Attribute("Name").Value, DeployEnvironmentName = dc.Attribute
("DeployEnvironmentName").Value })
});
^^^
});
You shouldn't use semicolons in object initializers, you should separate the properties with comma.
Your XML file seems to be broken. The Workflowtag was never closed.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have three classes :
public class QData
{
public List<RData> Items { get; set; }
public List<QDay> Dates { get; set; }
}
public class QDay
{
public string Date { get; set; }
public List<RData> Details { get; set; }
}
public class RData
{
public string Name { get; set; }
public int Quantity { get; set; }
}
my list is
List<QData> myList;
What is the most effective way to sort the list (*QData type) by Date, the Date is string.
Perhaps this is what you need:
var result = myList.Select(qData => new QData()
{
Items = qData.Items,
Dates = qData.Dates.OrderBy(qDay => DateTime.Parse(qDay.Date)).ToList();
}).ToList();
With DateTime.Parse call being perhaps modified to fit to the date format in the qDay.Date property.
Here is an example that sort using the first date in the Dates list. I can't imagine why you would ever want to do this but here it is. I suspect that having Dates be a list is a mistake, in fact you only want one date there.
var sortedList = MyList.OrderBy(element => DateTime.Parse(element.Dates.First().Date));
I think this is what you actually want... ONLY ONE LIST:
public class QData
{
RData itemInfo { get; set;}
QDay dateInfo { get; set; }
}
Then your sort would look like this:
var sortedList = MyList.OrderBy(element => DateTime.Parse(element.dateInfo.Date));
var temp = (from e in myList.Dates
orderby DateTime.Parse(e.Date)
select e
).ToList();
myList.Dates = temp;