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 ="
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 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; }
}
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 2 years ago.
Improve this question
How can I define classes with properties where the following code would compile and be valid?
AtomEntry newPost = new AtomEntry();
newPost.Title.Text = "Marriage!";
newPost.Content = new AtomContent();
newPost.Content.Content = "<div xmlns='http://www.w3.org/1999/xhtml'>" +
"<p>Mr. Darcy has <em>proposed marriage</em> to me!</p>" +
"<p>He is the last man on earth I would ever desire to marry.</p>" +
"<p>Whatever shall I do?</p>" +
"</div>";
newPost.Content.Type = "xhtml";
This should do it:
public class AtomEntry
{
public AtomContent Content { get; set; }
public Title Title { get; set; }
}
public class AtomContent
{
public string Content { get; set; }
public string Type { get; set; }
}
public class Title
{
public string Text { get; set; }
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
I've an object list with a property that is sublist (object list)
public class Class2
{
public String Item1 { get; set; }
public int Item2 { get; set; }
}
public class Class1
{
public String ItemA { get; set; }
public List<Class2> Classes2 { get; set; }
}
List<Class1> classes1 = new List<Class1> ();
I would like to find all existing Class2.Item1 in classes1.
Is it possible in linq c# ?
thks
All distinct? However, you can use SelectMany:
var allDistinctItem1 = classes1.SelectMany(x => x.Classes2.Select(y => y.Item1)).Distinct();
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 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
my task is to group the data from a list of Subscription object which has the following structure:
public class Subscription
{
public string ApplicationId { get; private set; }
public string UserCode { get; private set; }
public string SubscriptionId { get; private set; }
public string SubscriptionCode { get; private set; }
public DateTime? StartDate { get; private set; }
public DateTime? EndDate { get; private set; }
}
The code return a list of subscription, so List. In my case I've several items with same ApplicationId but different StartDate and EndDate. Basically I have to group these items
in order to create a json structure like this:
"applicationId" : {
"subscriptions" : [
{
"startdate" : ...,
"enddate" : ...,
},
{
"startdate" : ...,
"enddate" : ...,
},
]
}
Can I achieve this using LINQ?
Try this:
subscriptions.GroupBy(item => item.SubscriptionCode)
.Select(group => new { Subscription = group.Key, Items = group.Select(item => new { StartDate = item.StartDate, EndDate = item.EndDate}) })
Not sure if it will work but it reads like it will.
subscriptions is just a list of your object.