First of all sorry if i put the question in wrong section... :)
In sql server the following code adds varchar values separated by a comma sign and you can see the output in the image attached... [I found the code somewhere on the web] - CREATE TABLE #YourTable ([ID] INT, [Name] CHAR(1))
-
-
INSERT INTO #YourTable ([ID],[Name]) VALUES (1,'A')
-
INSERT INTO #YourTable ([ID],[Name]) VALUES (1,'B')
-
INSERT INTO #YourTable ([ID],[Name]) VALUES (2,'C')
-
-
select * from #YourTable
-
-
SELECT
-
[ID]
-
, REPLACE(RTRIM((SELECT [Name] + ' ' FROM #YourTable WHERE (ID = Results.ID) FOR XML PATH (''))),' ',', ') AS NameValues
-
FROM #YourTable Results
-
GROUP BY ID
-
-
DROP TABLE #YourTable
Now my question is how do i perform this operation using C# LINQ queries when i have a datatable or a table in sql server and i want that kind of output.
2 2078 -
E.g. you've got this data-source
-
-
List<KeyValuePair<int, char>> kvps =
-
new List<KeyValuePair<int, char>>();
-
-
kvps.Add(new KeyValuePair<int,char>(1, 'A'));
-
kvps.Add(new KeyValuePair<int,char>(1, 'B'));
-
kvps.Add(new KeyValuePair<int,char>(2, 'C'));
-
If it's a a datatable, there would be some slight differences, but generally this way should work -
var strs = from kvp in kvps
-
let str = kvp.Value.ToString()
-
group str by kvp.Key into grp
-
select new {
-
grp.Key,
-
CSV = string.Join(", ", grp.ToArray())
-
};
-
It should be obvious, what's going on, but I give a few hints: if there were strings instead of chars, the second line (let str = ...) can be omitted and instead of group str by ... you could use group kvp.Value by ...
At least there will be created an anonymous type have two properties Key and CSV
Thanks Mate... :)
In my case the data will flow from a linq datacontext and also little bit complected as it contains 4 joins... but i got the general idea... and will let you know after trying it....
@ChBinder : But it'll not work when data comes from sql server as string.Join(", ", grp.ToArray()) can't be translated into sql queries by linq....
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Marc Gravell |
last post by:
In Linq, you can apparently get a meaningful body from and
expression's .ToString(); random question - does anybody know if linq
also includes a parser? It just seemed it might be a handy way to...
|
by: fiona |
last post by:
Reading, Berkshire, UK 05 June 2007 - Crainiate Software make details
available of the release of Objecto Framework 2.0, an upgrade to their
enterprise business component framework, designed to...
|
by: Alexander Vasilevsky |
last post by:
Linq "into" and "let" equally???
http://www.alvas.net - Audio tools for C# and VB.Net developers
|
by: 0to60 |
last post by:
Let's say we have your basic Invoices and InvoiceItems table. If we load
this in with LINQ:
var query = from i in db.Invoices
select i;
When I then loop through my invoices, if I wanna...
|
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= |
last post by:
After having played around with LINQ and reading the literature on it I have
not been successful in getting this to work so I am looking for some guidance:
I want to find a hunk in a collection...
|
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...
|
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
|
by: =?Utf-8?B?SmFtZXMgUGFnZQ==?= |
last post by:
Hi all another LINQ question!!
to retrieve and display sql varbinary images I currently use the following
code:
Imports System.Data.SqlClient
Imports System.Drawing
Imports...
|
by: titli |
last post by:
Hi Friends,
I have some code in an already built application.
Now the output pivots in .xls files is not matching with input data.
The output .xls has summarized results whereas , the input...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
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,...
|
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$) {
}
...
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |