Find all items in all sublist in [closed] - c#

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();

Related

Instantiating an object in one line [closed]

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 ="

How to define a C# class model for these dataobjects [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 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; }
}

how to do grouping of the data.I want to show a list which on click contains data of foo and on click of it data of boo [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 4 years ago.
Improve this question
I have a resobject with structure like this.
public class resobject
{
public IList<foo> Foo{get;set}
public string message{get;set;}
}
public class foo
{
public IList<boo> Boo{get;set}
}
public class boo
{
public string category{get;set;}
}
currently i am having resobject data.How to reach to boo class and access the data.
public class Program
{
public class resobject
{
public IList<foo> Foo{get;set;}
public string message{get;set;}
}
public class foo
{
public IList<boo> Boo{get;set;}
}
public class boo
{
public string category{get;set;}
}
public static void Main()
{
var root = new resobject();
foreach(var f in root.Foo)
{
foreach(var b in f.Boo)
{
b.category = "value";
}
}
}
}

LINQ groupby and json output [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.

Order list where date is string [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;

Categories