473,385 Members | 1,553 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.

Query Over LINQ

33
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]
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE #YourTable ([ID] INT, [Name] CHAR(1))
  2.  
  3. INSERT INTO #YourTable ([ID],[Name]) VALUES (1,'A')
  4. INSERT INTO #YourTable ([ID],[Name]) VALUES (1,'B')
  5. INSERT INTO #YourTable ([ID],[Name]) VALUES (2,'C')
  6.  
  7. select * from #YourTable
  8.  
  9. SELECT 
  10.       [ID]
  11.     , REPLACE(RTRIM((SELECT [Name] + ' ' FROM #YourTable WHERE (ID = Results.ID) FOR XML PATH (''))),' ',', ') AS NameValues
  12. FROM #YourTable Results
  13. GROUP BY ID
  14.  
  15. 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.
Attached Images
 
Jun 1 '10 #1
2 2078
Christian Binder
218 Expert 100+
Expand|Select|Wrap|Line Numbers
  1. E.g. you've got this data-source
  2.  
  3. List<KeyValuePair<int, char>> kvps = 
  4.   new List<KeyValuePair<int, char>>();
  5.  
  6. kvps.Add(new KeyValuePair<int,char>(1, 'A'));
  7. kvps.Add(new KeyValuePair<int,char>(1, 'B'));
  8. kvps.Add(new KeyValuePair<int,char>(2, 'C'));
  9.  
If it's a a datatable, there would be some slight differences, but generally this way should work

Expand|Select|Wrap|Line Numbers
  1. var strs = from kvp in kvps
  2.            let str = kvp.Value.ToString()
  3.            group str by kvp.Key into grp
  4.            select new { 
  5.              grp.Key, 
  6.              CSV = string.Join(", ", grp.ToArray()) 
  7.            };
  8.  
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
Jun 1 '10 #2
sanndeb
33
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....
Jun 1 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

28
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...
0
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...
11
by: Alexander Vasilevsky | last post by:
Linq "into" and "let" equally??? http://www.alvas.net - Audio tools for C# and VB.Net developers
1
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...
5
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...
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...
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
4
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...
1
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...
0
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...
0
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...
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:
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.