linq to sql/xml - generate xml for linked tables - sql

i have alot of tables with alot of columns and want to generate xml using linq without having to specify
the column names. here's a quick example:
users
---------------
user_id
name
email
user_addresses
---------------
address_id
user_id
city
state
this is the xml i want to generate with linq would look like
<user>
<name>john</name>
<email>john#dlsjkf.com</email>
<address>
<city>charleston</city>
<state>sc</state>
</address>
<address>
<city>charlotte</city>
<state>nc</state>
</address>
</user>
so i'm guessing the code would look something like this:
var userxml = new XElement("user",
from row in dc.Users where user.id == 5
select (what do i put here??)
);
i can do this for one table but can't figure out how to generate the xml for a linked table (like user_addresses).
any ideas?

ok found a way to get the xml i want, but i have to specify the related table names in the query...which is good enough for now i guess. here's the code:
XElement root = new XElement("root",
from row in dc.users
where row.user_id == 5
select new XElement("user",
row.AsXElements(),
new XElement("addresses",
from row2 in dc.user_addresses
where row2.user_id == 5
select new XElement("address", row2.AsXElements())
)
)
);
// used to generate xml tags/elements named after the table column names
public static IEnumerable<XElement> AsXElements(this object source)
{
if (source == null) throw new ArgumentNullException("source");
foreach (System.Reflection.PropertyInfo prop in source.GetType().GetProperties())
{
object value = prop.GetValue(source, null);
if (value != null)
{
bool isColumn = false;
foreach (object obj in prop.GetCustomAttributes(true))
{
System.Data.Linq.Mapping.ColumnAttribute attribute = obj as System.Data.Linq.Mapping.ColumnAttribute;
if (attribute != null)
{
isColumn = true;
break;
}
}
if (isColumn)
{
yield return new XElement(prop.Name, value);
}
}
}
}

You need to use a join. Here's one way:
var query = from user in dc.Users
from addr in dc.UserAddress
where user.Id == addr.UserId
select new XElement("user",
new XElement("name", user.Name),
new XElement("email", user.Email),
new XElement("address",
new XElement("city", addr.City),
new XElement("state", addr.State)));
foreach (var item in query)
Console.WriteLine(item);
i have alot of tables with alot of
columns and want to generate xml using
linq without having to specify the
column names.
Not quite sure how you want to achieve that. You need to state the column names that go into the XML. Even if you were to reflect over the field names, how would you filter the undesired fields out and structure them properly without specifying the column names? For example how would you setup the address part? You could get the fields by using this on your User and UserAddress classes: User.GetType().GetFields() and go through the Name of each field, but then what?

Related

Get Database VIEW metadata via JDBC

I want to fetch metadata information regarding a Database view.
I can find my view using the following code
DatabaseMetaData databaseMetaData = connection.getMetaData();
String tableName = "";
ResultSet rSet = databaseMetaData.getTables(null, databaseName, null, new String[] {TABLE, VIEW});
while (rSet.next()) {
tableName = rSet.getString(TABLE_NAME_COLUMN);
if (rSet.getString(TABLE_TYPE_COLUMN).equals(TABLE)) {
tableNames.add(tableName);
} else if (rSet.getString(TABLE_TYPE_COLUMN).equals(VIEW)) {
viewNames.add(tableName);
}
}
Now when i have the view name, i want to fetch the list of data columns in the VIEW.
I use the following code
ResultSet rSet = databaseMetaData.getColumns(null, databaseName, table, null);
while (rSet.next()) {
columnDefinitions.add(new ColumnDefinition(rSet.getString(COLUMN_NAME_COLUMN), getColumnClassName(rSet
.getInt(DATA_TYPE_COLUMN))));
}
the concern here being that this query only returns the list of base columns from which this VIEW was derived. Say if my base table had 3 columns and i created a view if that table with 2 extra columns deriving information from the 3 base columns.
I want to fetch all 5 columns name and information. Is this possible ?
Is there any other method available to do this ?

Group By Sum Linq to SQL in C#

Really stuck with Linq to SQL grouping and summing, have searched everywhere but I don't understand enough to apply other solutions to my own.
I have a view in my database called view_ProjectTimeSummary, this has the following fields:
string_UserDescription
string_ProjectDescription
datetime_Date
double_Hours
I have a method which accepts a to and from date parameter and first creates this List<>:
List<view_UserTimeSummary> view_UserTimeSummaryToReturn =
(from linqtable_UserTimeSummaryView
in datacontext_UserTimeSummary.GetTable<view_UserTimeSummary>()
where linqtable_UserTimeSummaryView.datetime_Week <= datetime_To
&& linqtable_UserTimeSummaryView.datetime_Week >= datetime_From
select linqtable_UserTimeSummaryView).ToList<view_UserTimeSummary>();
Before returning the List (to be used as a datasource for a datagridview) I filter the string_UserDescription field using a parameter of the same name:
if (string_UserDescription != "")
{
view_UserTimeSummaryToReturn =
(from c in view_UserTimeSummaryToReturn
where c.string_UserDescription == string_UserDescription
select c).ToList<view_UserTimeSummary>();
}
return view_UserTimeSummaryToReturn;
How do I manipulate the resulting List<> to show the sum of the field double_Hours for that user and project between the to and from date parameters (and not separate entries for each date)?
e.g. a List<> with the following fields:
string_UserDescription
string_ProjectDescription
double_SumOfHoursBetweenToAndFromDate
Am I right that this would mean I would have to return a different type of List<> (since it has less fields than the view_UserTimeSummary)?
I have read that to get the sum it's something like 'group / by / into b' but don't understand how this syntax works from looking at other solutions... Can someone please help me?
Thanks
Steve
Start out by defining a class to hold the result:
public class GroupedRow
{
public string UserDescription {get;set;}
public string ProjectDescription {get;set;}
public double SumOfHoursBetweenToAndFromDate {get;set;}
}
Since you've already applied filtering, the only thing left to do is group.
List<GroupedRow> result =
(
from row in source
group row by new { row.UserDescription, row.ProjectDescription } into g
select new GroupedRow()
{
UserDescription = g.Key.UserDescription,
ProjectDescription = g.Key.ProjectDescription,
SumOfHoursBetweenToAndFromDate = g.Sum(x => x.Hours)
}
).ToList();
(or the other syntax)
List<GroupedRow> result = source
.GroupBy(row => new {row.UserDescription, row.ProjectDescription })
.Select(g => new GroupedRow()
{
UserDescription = g.Key.UserDescription,
ProjectDescription = g.Key.ProjectDescription,
SumOfHoursBetweenToAndFromDate = g.Sum(x => x.Hours)
})
.ToList();

Retrieving Data in Webmatrix without 'foreach'

I want to retrieve a single item of data from my database to then use as a value for an input element.
Using the below will retrieve each instance of SubmittedBy in the DB so results in multiple results.
var UserId = WebSecurity.CurrentUserId;
var db = Database.Open("testDB");
var selectQueryString = "Select SubmittedBy FROM Posts WHERE UserId=#0";
var data = db.Query(selectQueryString, UserId);
#foreach(var row in data)
{
<input type="email" name="emailfrom" id="emailfrom" value="#SubmittedBy"/>
}
How do I retrieve SubmittedBy so it only gives the one result i.e. without the foreach loop?
Thanks in advance!!!
If by your data restriccions are you going to obtain 1 and only 1 value for an specific UserId, you could use
var SubmyttedValue = db.QueryValue(selectQueryString, UserId);
There is a method created specifically for this purpose called QuerySingle - just change your query like this:
var data = db.QuerySingle(selectQueryString, UserId);
I hope this helps!
Change your query like this:
Select SubmittedBy FROM Posts WHERE UserId=#0 LIMIT 1

Entity Framework - attribute IN Clause usage

I need to filter some Entities by various fields using "normal" WHERE and IN clauses in a query over my database, but I do not know how to do that with EF.
This is the approach:
Database table
Licenses
-------------
license INT
number INT
name VARCHAR
...
desired SQL Query in EF
SELECT * FROM Licenses WHERE license = 1 AND number IN (1,2,3,45,99)
EF Code
using (DatabaseEntities db = new DatabaseEntities ())
{
return db.Licenses.Where(
i => i.license == mylicense
// another filter
).ToList();
}
I have tried with ANY and CONTAINS, but I do not know how to do that with EF.
How to do this query in EF?
int[] ids = new int[]{1,2,3,45,99};
using (DatabaseEntities db = new DatabaseEntities ())
{
return db.Licenses.Where(
i => i.license == mylicense
&& ids.Contains(i.number)
).ToList();
}
should work

SQL to Insert data into multiple tables from one POST in WebMatrix Razor Syntax

I've got two form fields from which the user submits a 'category' and an 'item'.
The following code inserts the category fine (I modified it from the WebMatrix intro PDF) but I've no idea how to then insert the 'item' into the Items table. I'll also need to add the Id of the new category to the new item row.
This is the code that's working so far
#{ var db = Database.OpenFile("StarterSite.sdf");
var Category = Request["Category"]; //was name
var Item = Request["Item"]; //was description
if (IsPost) {
// Read product name.
Category = Request["Category"];
if (Category.IsEmpty()) {
Validation.AddFieldError("Category", "Category is required");
}
// Read product description.
Item = Request["Item"];
if (Item.IsEmpty()) {
Validation.AddFieldError("Item",
"Item type is required.");
}
// Define the insert query. The values to assign to the
// columns in the Products table are defined as parameters
// with the VALUES keyword.
if(Validation.Success) {
var insertQuery = "INSERT INTO Category (CategoryName) " +
"VALUES (#0)";
db.Execute(insertQuery, Category);
// Display the page that lists products.
Response.Redirect(#Href("~/success"));
}
}
}
I'm guessing/hoping this is a very easy question to answer so hopefully there isn't much more detail required - but please let me know if there is. Thanks.
There's a Database.GetLastInsertId method within WebMatrix which returns the id of the last inserted record (assuming it's an IDENTITY column you are using). Use that:
db.Execute(insertQuery, Category);
var id = (int)db.GetLastInsertId(); //id is the new CategoryId
db.Execute(secondInsertQuery, param1, id);