Hi I have a rest service which has a list of students each student has a datetime attached to there creation.
On my client side I want to order by this datetime from newest first, on my client side the code looks like this:
public FindStudent()
{
InitializeComponent();
string uriGroups = "http://localhost:8000/Service/Student";
XDocument xDoc = XDocument.Load(uriGroups);
And it lists students by first created, I thought for a moment I could have added in an orderby query into the foreach var node but I dont think that will work, is there anyway I could do this?
You can use Linq to XML as follows
var temp = from feed in xDoc.Descendants("Students")
orderby Convert.ToDateTime(feed.Element("CreatDate").Value) descending
select new Student
{
ID = Convert.ToInt32(feed.Element("ID").Value),
//rest of the properties
};
in order to sort something based on date and time you need to convert it to date and time in your for-each loop you just loop through the pure text taken from xml document it not going to get ordered since it text an but you can create a student class with varaibles Studentid, FirstName LastName" TimeAdded TimeAdded loop through the document again write every information to the object student add every object to the list sort the list the way you would like and then I think it should display in order that you want.
XDocument xDoc = XDocument.Load(uriGroups);
var sortedXdoc = xDoc.Descendants("Student")
.OrderByDescending(x => Convert.ToDateTime(x.Element("TimeAdded").Value));
Related
I have a DynamoDb table (named Fruit) with the following properties:
FruitId - string
CreatedDate - date
Type - number
Payload - blob
I also have a local list of strings List<string> fruitIds;.
I want to query the Fruit table and get only the Ids that have a corresponding record (i.e. exist) in the table.
What is a good way of doing that? Right now, I am looping over each Id in fruitIds and making a separate query to DyanmoDb to see if I get a record back, if I do, I then save that Id to another local variable called fruitIdsThatExistInDyanmoDb.
Is there a better way?
public IQueryable <fruits> GetAllfruitsIDs() {
return fruits.AsQueryable();
}
var data = GetAllfruitsIDs();
// Or u can use this :
public IEnumerable<fruits> GetAllfruitsIDs() {
return fruits.AsQueryable().ToList;
}
var data = GetAllfruitsIDs();
Using Linq, its very simple, just check if item's FruitId is in fruitIds:
var result = fruits.Where(f => fruitIds.Contains(f.FruitId));
to save their Ids in a new local variable as you said:
List<string> fruitIdsThatExistInDyanmoDb = fruits.Where(f => fruitIds.Contains(f.FruitId))
.Select(f=> f.FruitId).ToList();
I have an accordion that binds data for each item from an array.
I want that for every time that I bound the data I will loop through all of the array and aggregate all of the items with the same id and create a long string with a name from a cell. In the code the name oneJob.order_id does not exist and I don't know why.
protected string GetAllProffesions(int orderID)
{
IEnumerable<string> allProf;
orderID = 544;
Job[] curItems = null;
curItems = JobManager.GetJobs(RangeID, GetParam());
allProf = from oneJob in curItems
where oneJob.order_id == orderID
select oneJob.profession_name;
return Convert.ToString(allProf);
}
This is because your job class doesn't have a property called order_id. Check your spelling.
Also, you probably don't want to do Convert.ToString(allProf), as I expect this will give you the type name instead of all the professions concatenated. Try this instead:
string.Join(", ", allProf.ToArray());
Scenario:
Grid view gets populated in WPF window.
Having a static list in code behind.(which i want to get from a xml file).
Trying to move the static list into an xml file.For that i created a ml file in the following format
<customers>
<customer Name="abc"/>
<customer Name="def"/>
</customers>
CodeBehind:
Xdocument doc=Xdocument.load("customers.xml");
var customerList = (from e in doc.Descendants("Cusomters")
select new
{
CustomerName = e.Attribute("Name").Value
}).ToList();
I am unable to get the customer names from the xml file to the customerList.I would appreciate if someone can help me to move forward.
"Cusomters" is spelled incorrectly, should be "Customers".
Obviously this is not the code your are using since it doesn't even compile. It should be this:
XDocument doc = XDocument.Load( "customers.xml" );
var customerList = (from e in doc.Descendants( "customer" )
select new
{
CustomerName = e.Attribute( "Name" ).Value
}).ToList();
You really should mention the fact that it won't compile. That, or you copied it in by hand incorrectly, which doesn't help us help you either.
The logical problem here is that you are asking for all Customers tags, note the s at the end. You really want to look for Customer tags, which have a name attribute. Customer*s* is simply the top level group.
Use customer instead of Cusomters (XML is case-sensitive):
from e in doc.Descendants("customer")
You most likely want a List<string> so you don't need to project to an anonymous class - also there is a typo in your query ("Cusomters"):
var customerList = (from e in doc.Descendants("Customer")
select e.Attribute("Name").Value).ToList();
or with extension method syntax:
var customerList = doc.Descendants("Customer")
.Select( e => e.Attribute("Name").Value)
.ToList();
Ive always used :
doc.root.elements("Customer")
for small snippets like this.
Greetings. Im having a small trouble i would like to have some help with. Im having a very large xml file with about 1000 customers with diffrent customer information. And I would like to do methods to retrive this information. Ive been searching everywhere but cant seem to find what im looking for. Currently im trying:
public custInformation getcustInfo(string file) {
//Load the xml file
var xe = XDocument.Load(_server.MapPath(file)).Root;
//Get information
return (from p in xe.Descendants("cust-account").Descendants("cust-info")
select new custInformation
{
firstName = (string)p.Element("cust-fname"),
lastName = (string)p.Element("cust-lname"),
address = (string)p.Element("cust-address1"),
}).(All elements)??
}
(All elements) is where id like to retrive all the information. Using FirstOrDefault will only retrive the first element and LastOrDefault will only retrive the first element. If some one could help me i would be very greatefull.
you want a list of customers. Change the return value to IEnumerable
and transform the query to IEnumerable with ToList()/ToArray():
public IEnumerable<custInformation> getcustInfo(string file) {
//Load the xml file
var xe = XDocument.Load(_server.MapPath(file)).Root;
//Get information
return (from p in xe.Descendants("cust-account").Descendants("cust-info")
select new custInformation
{
firstName = (string)p.Element("cust-fname"),
lastName = (string)p.Element("cust-lname"),
address = (string)p.Element("cust-address1"),
}).ToList();
}
Right now I have a very simple query that pulls up entries that have a string and a specific date range.
EventQuery eQuery = new EventQuery(calendarInfo.Uri.ToString());
eQuery.Query = "Tennis";
eQuery.StartDate = startDate;
eQuery.EndDate = endDate;
EventFeed myResultsFeed = _service.Query(eQuery);
After querying, myResultsFeed will contain an atomEntryCollection. Each atomEntry has a Title. The way I have it set up, there could be multiple entries with the same title.
I would like my Query to be able to select UNIQUE titles. Is this possible?
Link to the API Docs
I hypothesized that I could use a WHERE object
Where x = new Where();
x.yadayada();
but it can't be passed to _service.Query()
I'm also exploring the .extraparameters object. is it possible to do something like this?
eQuery.ExtraParameters = "distinct";
Looking into the "Partial Response" feature..
http://code.google.com/apis/gdata/docs/2.0/reference.html#PartialResponse
it looks pretty promising..
I don't think what you're trying to do is possible using the Google Data API.
However, extenting upon #Fueled answer, you could do something like this if you need a collection of AtomEntry's.
// Custom comparer for the AtomEntry class
class AtomEntryComparer : IEqualityComparer<AtomEntry>
{
// EventEntry are equal if their titles are equal.
public bool Equals(AtomEntry x, AtomEntry y)
{
// adjust as needed
return x.Title.Text.Equals(y.Title.Text);
}
public int GetHashCode(AtomEntry entry)
{
// adjust as needed
return entry.Title.Text.GetHashCode();
}
}
EventFeed eventFeed = service.Query(query)
var entries = eventFeed.Entries.Distinct(new AtomEntryComparer());
It's probably not the solution you were looking for, but since you have in hand an AtomEntryCollection (which down the line implements IEnumerable<T>), you could use LINQ to retrieve the distinct titles, like so:
EventFeed feed = service.Query(query);
var uniqueEntries =
(from e in feed.Entries
select e.Title.Text).Distinct();
And then loop over them with a simple foreach:
foreach (var item in uniqueEntries)
{
Console.WriteLine(item);
}
But then you have only a collection of string representing the Event titles, and not a collection of AtomEntry. I guess you could link them together in a Dictionary.
Not optimal, but should work.