473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

LINQ to SQL as XML

I have a LINQ class that I can use to query data from my database. I
want to query the data directly into an XML string without defining
each field.

If I have a table Names like the following:

Table: Names
Id int,
LastName varchar(32),
FirstName varchar(32)

Then I have a LINQ query that queries my DataContext:

var qry = from n in context.Names
where n.Id == pId
select n ;

I want to query the context as if to say
"select * from Names where Id = pId for XML AUTO"
If there a way to do that without manually creating an XElement?
Jun 27 '08 #1
3 1925
LINQ is intended to be an object pipeline - as such, there is no
*immediate* way to do this - i.e. you can't impact the auto-generated
SQL to that extent short of writing your own impementation. You could
issue direct SQL, but that might defeat the purpose...

However! One option might be to feed the LINQ output into LINQ-to-SQL;
the SQL query won't have "FOR XML AUTO" in it, but the result should
be largely the same...

For example (and borrowing an idea [AsXAttributes*] slightly from Jon
Skeet's book [I'm sure he'll forgive me])...

*=the idea is Jon's, but no concrete implementation is offered in the
book (it may well be on the website - I didn't look); so any fault in
this alternative implementation is mine, not his - i.e. credit =Jon,
blame =me ;-p

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;
using ConsoleApplication13;
static class Program
{
static void Main()
{
Application.EnableVisualStyles();
using (NWindDataContext ctx = new NWindDataContext())
{
ctx.Log = Console.Out; // show SQL
var suppliers = new XElement("suppliers",
from supplier in ctx.Suppliers
select new XElement(
"supplier",
supplier.AsXAttributes())
);
Console.WriteLine(suppliers);
}
}

static IEnumerable<XAttribute>
AsXAttributes(this object source)
{
if(source == null) throw new ArgumentNullException("source");
foreach (PropertyDescriptor prop in
TypeDescriptor.GetProperties(source))
{
object value = prop.GetValue(source);
if (value != null)
{
yield return new XAttribute(prop.Name, value);
}
}
}
}
Jun 27 '08 #2
LINQ output into LINQ-to-SQL
Should have read: LINQ-to-SQL output into LINQ-to-XML

Marc
Jun 27 '08 #3
On Apr 12, 4:41*pm, Marc Gravell <marc.grav...@gmail.comwrote:

<snip>
For example (and borrowing an idea [AsXAttributes*] slightly from Jon
Skeet's book [I'm sure he'll forgive me])...

*=the idea is Jon's, but no concrete implementation is offered in the
book (it may well be on the website - I didn't look); so any fault in
this alternative implementation is mine, not his - i.e. credit =Jon,
blame =me ;-p
Our code is almost identical - except I've just used PropertyInfo
instead of PropertyDescriptor. Oh, and I replace "_" with "-" in the
name, for the sake of some particular example I was interested in. I
also didn't bother with the null argument check - you're more
fastidious than I am, clearly :)

Here's the code (downloaded from http://csharpindepth.com/Downloads.aspx
as I don't have the original with me at the moment):

public static IEnumerable<XAttributeAsXAttributes(this object data)
{
foreach (PropertyInfo prop in data.GetType().GetProperties())
{
object value = prop.GetValue(data, null);
if (value != null)
{
yield return new XAttribute
(prop.Name.Replace("_", "-"), value);
}
}
}

Jon
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dave Johnson | last post by:
Greetings, i want to be able to use linq new technology with sql server. the senario i am not able to do so far is as follow: 1- i program with linq 2- be able to generate and manipulate...
4
by: cj | last post by:
Can I use LINQ in VB .Net 2008 to get to a Visual FoxPro Table? I've seen demos of it with SQL Server but they've used it with a server browser and I don't think that'd work when I need to see VFP...
9
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
Hi all: after reading different places/sites about linq... I ran into these questions: 1. What framework do we need to run linq ? (does it depend on what version of visual studio we have?) how...
0
by: =?Utf-8?B?SHlwZXJjb2Rlcg==?= | last post by:
I'm encountering some strange behavior after deploying a ASP.net 3.5 website to production, i'm unable to reproduce these in my dev environment. This error seems to occur very randomly but it's...
4
by: =?Utf-8?B?RXJpYyBGYWxza2Vu?= | last post by:
We’re storing our main entity in an insert only table which stores the history of past revisions, but we’re facing problems with storing this history as LINQ will only update the entity, and...
14
by: thj | last post by:
Hi, I was wondering what you guys are using and why? LINQ to SQL or NHibernate? Thanks in advance, Tommy
9
by: Cirene | last post by:
I'm about to begin a brand new, big, ASP.NET project (using 3.5 .net fw), VS 2008. I'm using MySQL as the backend (customer request.) I have absolutely no experience with LINQ and/or the Entity...
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
4
by: George | last post by:
I am a bit conservative type and usually give some time for technology to mature before starting to try it. Today my question is Linq. To start using it or not. So here is the voting questions....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.