Super-simple linq query still doesn't work - c#

Trying to use Linq and the Entity Framework to make a super-simple lists database. The query runs, but it does not return the data I enter into it:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using static System.Console;
namespace CheckList
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("Building the list.");
using (var db = new ListContext())
{
ListItem socks = new ListItem() { Title = "Get Socks" };
db.ListItems.Add(socks);
Here is the actual query:
var queryresults = from item in db.ListItems
orderby item.Title
select item;
But the foreach loop doesn't print out anything:
WriteLine("Printing out the list:");
foreach (var item in queryresults)
{
WriteLine("Item's name:");
WriteLine(item.Title);
}
The rest:
}
WriteLine("All done.");
Console.Read();
}
}
}
I've tried to simplify as much as possible, but I cannot seem to get the results to show up. What is my mistake here?

Before the query try to save changes.
db.SaveChanges();

Related

Select IFC Physical Simple Quantity of Wall

I'm using XBim IFC libraries in order to get some info of a Building model elements.
Specifically, of IfcWall entities.
I have to acces Wall Base Quantities (lenght, height, width, etc.) but I cant reach those properties from IfcWall class.
I have this class:
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xbim.Ifc;
using Xbim.Ifc4.ActorResource;
using Xbim.Ifc4.DateTimeResource;
using Xbim.Ifc4.ExternalReferenceResource;
using Xbim.Ifc4.PresentationOrganizationResource;
using Xbim.Ifc4.GeometricConstraintResource;
using Xbim.Ifc4.GeometricModelResource;
using Xbim.Ifc4.GeometryResource;
using Xbim.Ifc4.Interfaces;
using Xbim.Ifc4.Kernel;
using Xbim.Ifc4.MaterialResource;
using Xbim.Ifc4.MeasureResource;
using Xbim.Ifc4.ProductExtension;
using Xbim.Ifc4.ProfileResource;
using Xbim.Ifc4.PropertyResource;
using Xbim.Ifc4.QuantityResource;
using Xbim.Ifc4.RepresentationResource;
using Xbim.Ifc4.SharedBldgElements;
namespace ProcesadorPremoldeado.IFC
{
public class IFCCalculos
{
public void CalculoPlacas(string fileName, XbimEditorCredentials editor)
{
using (var model = IfcStore.Open(fileName, editor))
{
using (var transaction = model.BeginTransaction("Quick start transaction"))
{
//get all Walls in the model
var ifcWallsList = model.Instances.OfType<IfcWall>();
foreach (var wall in ifcWallsList)
{
var prop = wall.PhysicalSimpleQuantities.Where(x=>x.Name=="Height");
}
transaction.Commit();
}
}
}
}
}
That lambda expression return me a row, correctly filtered by Name parameter, as this property is accessible.
But I cant access the property call "LengthValue", the strange thing is that the property is visible during debbugin if I put a breakpoint, under "prop" list in the foreach loop.
Anyone could give me an idea of what could be happening?
Thanks in advance!
This is because LengthValue is a property of IfcQuantityLength, but PhysicalSimpleQuantities are of supertype IfcPhysicalQuantity. You just need to select quantities of the right type
var height = wall.PhysicalSimpleQuantities
.OfType<IfcQuantityLength>()
.Where(x=>x.Name=="Height")?
.LengthValue;

fetching only one url in c#

I am working on collecting urls from the web site in C# using WatiN framework. In my program it is fetching only one url. I don't know what is the problem. Any help will be appreciated.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WatiN.Core;
using WatiN.Core.Native.InternetExplorer;
namespace magicbricks
{
class scroll
{
[STAThread]
static void Main(string[] args)
{
Browser browserInstance;
browserInstance = new IE(#"http://www.99acres.com/property-in-chennai- ffid?search_type=QS&search_location=CP32&lstAcn=CP_R&lstAcnId=32&src=CLUSTER&isvoicesearch=N&keyword_suggest=chennai%20%28all%29%3B&fullSelectedSuggestions=chennai%20%28all%29&strEntityMap=W3sidHlwZSI6ImNpdHkifSx7IjEiOlsiY2hlbm5haSAoYWxsKSIsIkNJVFlfMzIsIFBSRUZFUkVOQ0VfUywgUkVTQ09NX1IiXX1d&texttypedtillsuggestion=chennai&refine_results=Y&Refine_Localities=Refine%20Localities&action=%2Fdo%2Fquicksearch%2Fsearch&suggestion=CITY_32%2C%20PREFERENCE_S%2C%20RESCOM_R");
foreach (var links in browserInstance.Links.Filter(Find.ByClass("b")))
{
Console.WriteLine(links.Url);
String filePath = "C:/Users/User/Desktop/New folder";
String fileName = "newop4.csv";
using (StreamWriter sr = new StreamWriter(Path.Combine(filePath, fileName), true))
{
sr.WriteLine(links.Url);
}
Console.ReadLine();
}
}
}
}
the above code prints only one url in the console.
Remove the Console.ReadLine(); As you are in a ForEach loop. If you still want the Console.ReadLine(); move it out the foreach
The Console.ReadLine(); waits for a user input, after you enter any value you should see the next URL.

How to print out the result of a SWI-Prolog query from C# using Swi-cs-pl library

I'm trying to understand how the Swi-cs-pl library works by doing my own little program, but I cannot printout any result from a query based on the example from SWI-Prolog web.
I have this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SbsSW.SwiPlCs;
namespace HolaMundo
{
class Program
{
static void Main(string[] args)
{
if (!PlEngine.IsInitialized)
{
String[] param = { "-q" }; // suppressing informational and banner messages
PlEngine.Initialize(param);
PlQuery.PlCall("assert(father(hader, nevar))");
PlQuery.PlCall("assert(father(hader, sergio))");
PlQuery.PlCall("assert(brother(nevar, sergio):- father(hader, nevar), father(hader, sergio))");
//How do I write out in a Console the answer for a query like:
// brother(nevar, sergio)
PlEngine.PlCleanup();
}
}
}
}
As I said on my code, I just want to query something basic like: brother(nevar, sergio). and get any answer from Console like true or whatever.
Can anyone help me?

getting all properties names and values from Directory Entry object

I am getting all websites from localhost IIS manager 6 using DirectoryEntry class, I would like to get local path of each web application, but not sure how to get it, Is there anyway I can enumerate all properties of directory entry ?
DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
foreach (DirectoryEntry e in root.Children)
{
if (e.SchemaClassName == "IIsWebServer")
{
Console.WriteLine(e.Properties["ServerComment"].Value.ToString());
// how can I enumerate all properties and there values here ?
// maybe write to a xml document to find the local path property
I think you can use following code to find what you need. In case other questions just use the same approach. This code works for IIS6.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.DirectoryServices;
namespace IIS_6
{
class Program
{
static void Main(string[] args)
{
DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
string VirDirSchemaName = "IIsWebVirtualDir";
foreach (DirectoryEntry e in root.Children)
{
foreach (DirectoryEntry folderRoot in e.Children)
{
foreach (DirectoryEntry virtualDirectory in folderRoot.Children)
{
if (VirDirSchemaName == virtualDirectory.SchemaClassName)
{
Console.WriteLine(String.Format("\t\t{0} \t\t{1}", virtualDirectory.Name, virtualDirectory.Properties["Path"].Value));
}
}
}
}
}
}
}
With regards to IIS 7 I wrote this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// to add it from %windir%\System32\InetSrv\Microsoft.Web.Administration.dll
using Microsoft.Web.Administration;
namespace IIS_7
{
class Program
{
static void Main(string[] args)
{
using (ServerManager serverManager = new ServerManager())
{
foreach (var site in serverManager.Sites)
{
Console.WriteLine(String.Format("Site: {0}", site.Name));
foreach (var app in site.Applications)
{
var virtualRoot = app.VirtualDirectories.Where(v => v.Path == "/").Single();
Console.WriteLine(String.Format("\t\t{0} \t\t{1}", app.Path, virtualRoot.PhysicalPath));
}
}
}
}
}
}

I am using LINQ to connect to a database and i get a list of properties and I want to convert it to an XML File

Below is what I use to log into the database using linq and then I use C# expressions to gather the data I want. The next thing I want to do is convert this data into an XML any Ideas?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Data;
namespace VcacManagementTools.BuildProfiles
{
public static class BuildProfileTools
{
public static ICollection<string> GetExistingBuildProfileNames(string repositoryHostname,
string repositoryUsername,
string repositoryPassword)
{
var url = string.Format("https://{0}/repository/data/ManagementModelEntiti.svc", repositoryHostname);
var managementModelClient = new DynamicOps.ManagementModel.ManagementModelEntities(new Uri(url))
{
Credentials = new NetworkCredential(repositoryUsername, repositoryPassword)
};
return managementModelClient
.GlobalProfiles
.Select(gp => gp.ProfileName)
.ToList();
The Output I recieve is a list of values
If I understood you well, you want to take the data (the list contains the data from the database) and put it in XML file. I used variables to show where to put each data.
In case you have an XML:
try
{
doc = XDocument.Load(spath, LoadOptions.SetBaseUri);
foreach(String propertyData in dataList)
{
XElement root = new XElement(ElementName);
root.Add(new XElement("property1", propertyData));
doc.Element(MainElement).Add(root);
}
doc.Save(spath);
}
catch (Exception)
{
}

Categories

Resources