Return entire collection from MongoDB - c#

using the official mongo / c# drivers - what is the best way of returning an entire collection, and what is the best way of storing the data? I've seen some examples of iterating over a collection and returning a particular value, like this:
var collection = db.getCollection("users").findAll();
foreach (var value in collection){
value = collection["key"];
...
}
but what if I don't know the key names - and I just want to return the collection?

You dont need to know the key names when returning a collection.
public static void ReadCollectionDataUsingBson(string collectionName, string databaseName)
{
MongoDatabase database = CreateDatabase(databaseName);
MongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>(collectionName);
foreach (BsonDocument document in collection.FindAll())
{
foreach (string name in document.Names)
{
BsonElement element = document.GetElement(name);
Console.WriteLine("{0}: {1}", name, element.Value);
}
Console.WriteLine();
}
}
Note: CreateDatabase() function is user defined, so i have just shown you the required code over here.

Related

Use of using statement for Entity Framework DbContext in foreach-loop

There is a parser that parses a text file which contains object definition. The object definitions in the text file have a placeholder handle key. The place holder handle needs to be replaced with actual value by looking up the handle value in DB. In my application I am making use of the Entity framework Core for working with the DB.
The parser returns one object at a time, and I am looking up the handle and other properties in the DB one at a time. This is how the code looks so far:
IEnumerable<ObjectInfo> GetNextContent();
IEnumerable<ObjectInfo> GetNextObjectInfo()
{
foreach (var item in parser.GetNextContent())
{
using (var dbContext = new ContentDbContext())
{
string key = item.Key;
string id = dbContext.Contents.Find(key).ObjectId;
item.Id = id;
// Assign other fields...
yield return item;
}
}
}
The question that I have is that in the code above, the 'using' block is within the foreach loop.
Is this a right thing to do?
The other thought is that I can take the 'using' block outside of the foreach-loop but then I am not sure how would that play out with the iterator in the code.
You should move ContentDbContext into outside for better performance.
This is simply because You just need one context per request.
One DbContext per web request... why?
using (var dbContext = new ContentDbContext())
{
foreach (var item in parser.GetNextContent())
{
string key = item.Key;
string id = dbContext.Contents.Find(key).ObjectId;
item.Id = id;
// Assign other fields...
yield return item;
}
}
Updated
You might also join then make sure that fetch all data at a time
// You need to fetch all `item.Key` from `parser.GetNextContent()` to get all data in `dbContext.Contents`
var keys = parser.GetNextContent().Select(p => p.Key).ToArray();
var result = (from content in dbContext.Contents
join key in keys on content.Id equals key
select new
{
Id = content.ObjectId,
//....
}
If you are use C# 8, using statement may be as below:
using var dbContext = new ContentDbContext();
foreach (var item in parser.GetNextContent())
{
string key = item.Key;
string id = dbContext.Contents.Find(key).ObjectId;
item.Id = id;
// Assign other fields...
yield return item;
}

Specified Cast Not Valid - XamarinFirebase

This is my firebase database structure
Database Structure
In the child, there are values , Name, Country, Age and Uid. The Uid is the focus in my question.
I am try to fetch the children of the node (Chat) based on the current logged in user.
I am basically trying to do a comparison here that, firebase should get me children who only have the Uid = user.uid. i.e the current logged in user. I thought Equals() could do the trick but i get an error with the cast.
The error points at this line var items = snapshot.Children?.ToEnumerable<DataSnapshot>().Equals(user.uid).ToString;
public void OnDataChange(DataSnapshot snapshot)
{
var items = snapshot.Children?.ToEnumerable<DataSnapshot>().Equals(user.uid).ToString;
var key = snapshot.Key;
HashMap map;
foreach (DataSnapshot item in items)
{
}
Console
my snapshot test: DataSnapshot { key = Chat, value = {-KomfGbZxCGESgyo1PFT={Name=Testing , Ref=7YB3mxMRXxW4lzhbhxi1bx7K4Pf1,
}, -KomcJyR5dCxFucJSB0I={Name=Hi, Ref=K6TEpccn1TbB32T8ThFsYnIl6Wm2, }} }
If you want to filter the user based on Ref attribute, I suggest you make an if-statement inside foreach loop to filter the item:
public void OnDataChange(DataSnapshot snapshot)
{
var items = snapshot.Children?.ToEnumerable<DataSnapshot>();
HashMap map;
foreach (DataSnapshot item in items)
{
//filter the user first
map = (HashMap)item.Value;
string userId = map.Get("Ref")?.ToString();
if (userId!=null&& userId == user.uid)
{
//do what you want to do here.
}
}
When you use ToEnumerable, the type of returned objects is DataSnapshot and it cannot be compared to user.uid. You might want to use linq to grab what you wants. something like:
var items = snapshot.Children?.ToEnumerable(x=> x.uid == user.uid)

Update multiple fields in SharePoint programmatically

I'm trying to update some properties on multiple fields (with the same name) in SharePoint.
I've tried this:
var site = this.Site.RootWeb;
var fields = site.Fields;
foreach (SPField field in fields)
{
if (field.Group.Contains("My Custom Columns"))
{
if (field.Title.Contains("Custom field"))
{
if (field.DefaultValue != null) {
field.DefaultValue = null;
field.Update(true);
}
}
}
}
It updates the first column with the name "Custom field", but after it's giving me this error:
Collection was modified; enumeration operation may not execute.
at Microsoft.SharePoint.SPBaseCollection.SPEnumerator.System.Collections.IEnumerator.MoveNext()
Is it not possible to Update the object in a foreach loop?
This error occurs since you are trying to modify a field collection while iterating it.
The solution would be to replace the line:
foreach (SPField field in fields)
with
foreach (var field in fields.Cast<SPField>().ToList())
The problem I believe is with your
foreach (SPField field in fields)
line of code. You are essentially modifying the collection that you are looping over.
What I would suggest you try is looping and getting the ID's of all the fields in to a
List<GUID>
Then do a foreach statement on this collection getting each field and updating it's value.
List<Guid> guidsList = new List<guid>();
foreach (SPField field in fields)
{
if (field.Group.Contains("My Custom Columns"))
{
if (field.Title.Contains("Custom field"))
{
guidsList.add(field.id)
}
}
}
foreach(Guid currentFieldId in guidsList){
//Get your field
//Update what needs to be updated
}
Many Thanks
Truez

Match algorithm

I am unsure how take data from an IEnum object and dump it to a Dictionary and/or List, and would appreciate any guidance.
I'm working on a program that sorts through a collection of data pulled from SQL DB. I'm using Enterprise Library 5 (Data Access Application Block). So far there is an adapter class that will create an instance of the DB, map the query results to appropriate fields (Namely: First, middle, last names, DoB, and an ID), and it has two methods of type IEnumerable that will pass the SQL statement. I am unsure of how the the return IEnum object looks like. One method will return a list of offenders and another will return a list of all the records based on DoB:
public IEnumerable<Person> GetOffenders()
{
var people =
_db.ExecuteSqlStringAccessor<Person>(#"SELECT pm.Local_ID, pm.First_Name, pm.Last_Name, pm.Middle_Name, pm.DOB
FROM Person_Main pm ", _personRowMapper);
return people;
}
In another class that will hold the matching logic, I am trying to "dump" the contents of the getOffenders() which was defined in the adapter class, the idea is iterate over the two lists (offenders and all records based on DoB). I'm assuming the a good route would be to use a Dictionary to hold the results from getOffenders(). So far I've got this test code, which only checks to see if the connection to the DB is successful and retrieves a row of dummy data I entered:
PersonAdapter personAdapter = new PersonAdapter();
private Dictionary<String, List<Person>> testDictionary = new Dictionary<String, List<Person>>();
public void Test1()
{
var offenders = personAdapter.GetOffenders();
var pList = new List<Person>();
// Test to see if GetOffenders is returning data correctly
foreach (var variable in offenders)
{
var fName = variable.FirstName;
var mName = variable.MiddleName;
var lName = variable.LastName;
var lId = variable.LocalId;
Console.WriteLine(variable.FirstName + " " + variable.MiddleName + " " +
variable.LastName + " " + variable.LocalId);
Console.ReadLine();
}
An IEnumerable implementation only returns one item at a time. If you want to transfer those to a static collection you will need to loop through the IEnumerable collection and then do a List<Person>.Add(item).
foreach(var item in offenders)
{
pList.Add(item);
}
Then you can use your pList collection as a normal List<> to then add to your testDictionary field.
testDictionary.Add("some_string_key", pList);

Stuck with List<>

I have this:
public class accounts
{
private string mName;
private string mEmail;
private string mAddress;
public accounts(string Name,
string Email,
string Address)
{
this.mName = Name;
this.mEmail = Email;
this.mAddress = Address;
}
}
then, somewhere else, I create this:
private static List<accounts> mlocalaccountList = new List<accounts>()
then I fill it like this:
mlocalaccountList.Add(new accounts("John Smith","johnsmith#mail.com","CA USA"));
Now, everything is OK, except, how can I access the list<> items??
You can access them in a foreach loop:
foreach (var item in mlocalaccountList) {
...
}
however, since all members are private you cannot access them at all. Consider making properties for the private members or making them public.
You can also access them by index:
mlocalaccountList[0]
is the first item in the list.
By indexer like an array
mlocalaccountList[0]
foreach (accounts a in mlocalaccountList) { /* do something */ }
will iterate through the list.
Try mlocalaccountList[0] or
foreach (accounts acct in mlocalaccountList)
{
// Do something with acct
}
I would recommend using a foreach statement or just access by using an index variable mlocalaccount[index]
You can iterate over them:
foreach (var item in mlocalaccountList)
{
// do stuff with item
}
You can use LINQ:
var usaItems = mlocalaccountList.Where(a => a.Address.Contains("USA"));
// assuming you implement a public property for Address
Here's a link to the List<T> MSDN page. The Members page lists all the methods and properties that you have available. You can find help on ForEach for example.
The MSDN library (http://msdn.microsoft.com/en-us/library/) is an invaluable source of information on the classes and their members.
Just combining the list of everyone's answers here so far:
Use an indexer into the list: mlocalaccountsList[i] will return the i'th element (0-based index, of course)
Iterate over the list using a loop. foreach(var account in mlocalaccountList) will easily provide you with each element in turn.
Use a LINQ query to filter out a specific element in the list. LINQ has two different styles of writing queries:
var result = mlocalaccountList.Where(a => a.Name == "John Smith"))
// or
var result = from a in mlocalaccountList
where a.Name == "John Smith"
select a;
Use a foreach statement:
foreach (accounts acc in mlocalaccountList)
{
... do something with acc
}
Though I don't program in C#, I believe it is: mlocalaccountList[index] where index is an int.

Categories