vb.net to c#.net mongodb sort by date - c#

I'm trying to get a small snippet of code from vb.net to c#.net using mongoDB driver working. It's almost working, but I can't seem to get the doc out of the mongoCursor in the same way I do from vb.net.
vb.net code:
Dim latest = updateCollection.FindAll.SetSortOrder(SortBy.Descending("date")).SetLimit(1)
Dim latestDoc As BsonDocument = latest(0)
c#.net code:
var updateCollection = database.GetCollection<BsonDocument>("updateInfo");
var sortby = SortBy.Descending("date");
var latest = (updateCollection.FindAll().SetSortOrder(sortby).SetLimit(1));
var latestDoc = latest.ToBsonDocument();
I've tried something like
var latestDoc = latest[0];
as well, but it hasn't worked. What am I missing?
Thank you!

You're getting a MongoCursor, not an item back from that that method, so you'll want to do something like this:
//add at the top of your code file
using System.Linq;
//...
//get the first result from the cursor like this:
var latestDoc = latest.FirstOrDefault();

Related

Converting LinqToExcel.RowNoHeader type into string

I have been using LinqToExcel Library Lately and i came across this issue.
here is the code
var excel = new ExcelQueryFactory(fileName);
var dataList = (from c in excel.WorksheetRangeNoHeader("A1", "A4", "Test Worksheet")
select c).ToList();
This dataList Collection Will have something like "Cat","Dog" ,"Fish" and "Goat"
Now All i want to do is assign Fish to a variable as an string by doing something like this
string temp=dataList[2];
However when i run the code i get the value of temp as "LinqToExcel.RowNoHeader". Anyone got idea on how to extract the actual value Fish into the variable temp.
Thanks in Advance
Cracked it !
I was referring to the whole row rather than the first cell of each row.
Therefore i needed C[0] in order to get the items.
var excel = new ExcelQueryFactory(fileName);
var dataList = (from c in excel.WorksheetRangeNoHeader("A1", "A4", "Test Worksheet")
select c[0]).ToList();
And the extraction code will be
var temp=Convert.ToString(dataList[2]);

Paging of RSS using System.ServiceModel.Syndication

I'm trying to achieve pagination of the posts i get from a RSS-feed using the System.ServiceModel.Syndication. However I cant figure out how to do this and what the best way to do it is.
As of now i use a Listview to present the data that is fetched by this in my code-behind:
// Link to the RSS-feed.
string rssUri = "feed.xml";
var doc = System.Xml.Linq.XDocument.Load(rssUri);
// Using LINQ to loop out all posts containing the information i want.
var rssFeed = from el in doc.Elements("rss").Elements("channel").Elements("item")
select new
{
Title = el.Element("title").Value,
PubDate = el.Element("pubDate").Value,
Enclosure = el.Element("enclosure").Attribute("url").Value,
Description = el.Element("description").Value
};
// Binding the data to my listview, so I can present the data.
lvFeed.DataSource = rssFeed;
lvFeed.DataBind();
So where do I go from here? I'm guessing one way is to work with a DataPager in my Listview? However I'm uncertain how to work with that control, should I send all of my data into some list or something like IEnumerable<>?
After some trial and error and reading up on the DataPager i came up with the following solution that is now working very well!
First off i created a class for my object, working with that i set up a select-method for my ListView initiating at page-load binding the data to it. And the trick here was to use the ICollection interface, and send the data to a list. This is the now working code for that select-method, hope it can help someone else out facing the same problem! :)
ICollection<Podcast> SampleData()
{
string rssUri = "http://test.test.com/rss";
var doc = System.Xml.Linq.XDocument.Load(rssUri);
ICollection<Podcast> p = (from el in doc.Elements("rss").Elements("channel").Elements("item")
select new Podcast
{
Title = el.Element("title").Value,
PubDate = el.Element("pubDate").Value,
Enclosure = el.Element("enclosure").Attribute("url").Value,
Description = el.Element("description").Value
}).ToList();
return p;
}

BindindSource Filter does not work

Please see the following code on a Winform Load method:
List<CustomersDTO> res = new List<CustomersDTO>();
res = _CustomerBO.GetCustomers();
customerBindingSource.DataSource = res;
customerDataGridView.DataSource = this.customerBindingSource;
customerBindingNavigator.BindingSource = this.customerBindingSource;
Now I want to filter on Searchbutton but I am not able to see filtered record on screen.
customerBindingSource.Filter = "Active = false";
I am missing something.. I did reasearch. Can anybody give me exact code example? I read about implementing IBindingList but not sure how to do that with my BindingSource..
Can anyone help?
You don't have to implement IBindingList. You can construct a BindingList as the DataSource of your customerBindingSource. Like This:
customerBindingSource.DataSource = new BindingList<CustomersDTO>(res);

How to get the value of an input box of the webbrowser control in WPF?

I added Microsoft.mshtml as a reference to my project and came this far:
mshtml.IHTMLDocument2 document = (mshtml.IHTMLDocument2)webbrowser.Document;
string username = document.all["username"].GetAttribute("value");
but the 2nd line doesn't work. It says
"error CS0021: Cannot apply indexing with [] to an expression of type
'mshtml.IHTMLElementCollection'"
when hovering over "all". How do I access the elements in all?
Try this:
var document = (IHTMLDocument3) webbrowser.Document;
var value =
document.getElementsByName("username")
.OfType<IHTMLElement>()
.Select(element => element.getAttribute("value"))
.FirstOrDefault();
After struggling for a couple of hours this solutions worked for me.
Dim document = DirectCast(MainBrowser.Document, IHTMLDocument3)
Dim formName = document.getElementsByName(AppSettings("myFormName")).OfType(Of IHTMLElement)().Select(Function(element) element.getAttribute("name")).FirstOrDefault()

Filtering array in monoTouch with NSPredicate

So I'm trying to filter an array with NSPredicate in MonoTouch. In Objective-c it would be something like this:
NSPredicate *findStringWithReference = [NSPredicate predicateWithFormat:#"SELF CONTAINS [cd] %#",cRText.text];
NSArray *trackTraceContentFiltered = [trackTraceContent filteredArrayUsingPredicate:findStringWithReference];
I can't figure out how to do this in c#.
Do you have to use an NSArray or could you use a .Net List / Collection enabling you to use Linq?
101 LINQ Samples is a great resource for using LINQ.
Something like this:
NSPredicate findStringWithReference = NSPredicate.FromFormat("SELF CONTAINS [cd] %#", new NSObject[] {cRText.StringValue } );
var trackTraceContentFiltered = trackTraceContent.Filter(findStringWithReference);
If you are working in .NET, you can use LINQ. There are lots of examples online, something like this:
var array = new string[] { "one", "two", "three" };
var filteredResult = array.Where(x => x.Contains("ne");

Categories

Resources