Asp.net 如何在Sql server中删除重复的行值并在gridview中绑定它们
我有一个表作为产品colums productid、productname、product value、, 列中的值为Asp.net 如何在Sql server中删除重复的行值并在gridview中绑定它们,asp.net,Asp.net,我有一个表作为产品colums productid、productname、product value、, 列中的值为 productid productname productQuantity 1 a1 2 1 a2 2 1 a3 2 1 a4 2 1 a5 2 2 a21 3 2
productid productname productQuantity
1 a1 2
1 a2 2
1 a3 2
1 a4 2
1 a5 2
2 a21 3
2 a22 3
2 a23 3
因此,当我在gridview中绑定时,我需要得到如下输出
productid productname productQuantity
1 a1,a2,a3,a4,a5 2
2 a21,a22,a23 3
那么,当我绑定到gridview时,如何获得这种格式的数据呢。
有没有关于如何编写Sp以便获取此格式数据的帮助
那太好了。或者,有没有其他方法可以获得这种格式的数据
任何帮助都会很好
谢谢你这会让你非常接近(在.net 3.5中):
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间控制台应用程序1
{
公共类产品
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共整数数量{get;set;}
来自IDataReader的公共静态产品(System.Data.IDataReader阅读器)
{
产品=新产品();
product.Id=reader.GetInt32(reader.GetOrdinal(“ProductId”);
product.Name=reader.IsDBNull(reader.GetOrdinal(“ProductName”))?
string.Empty:reader.GetString(reader.GetOrdinal(“ProductName”);
product.Quantity=reader.IsDBNull(reader.GetOrdinal(“ProductQuantity”))?
0:reader.GetInt32(reader.GetOrdinal(“ProductQuantity”);
退货产品;
}
}
公共静态类LinqExtensions
{
公共静态字符串ToCsv(此IEnumerable items)
{
System.Text.StringBuilder sb=新的StringBuilder();
foreach(项中的字符串项)
{
某人附加(项目);
某人加上(“,”);
}
使某人返回字符串修剪(“”,’);
}
}
班级计划
{
静态IEnumerable GetProductsFromDatabase()
{
使用(System.Data.SqlClient.SqlConnection connection=new System.Data.SqlClient.SqlConnection(“某些连接字符串”))
{
字符串sql=@“返回productid、productname和productQuantity列的某些sql查询”;
使用(System.Data.SqlClient.SqlCommand=new System.Data.SqlClient.SqlCommand(sql,连接))
{
connection.Open();
使用(System.Data.IDataReader reader=command.ExecuteReader())
{
while(reader.Read())
从IDataReader(读卡器)返回的产品产量;
}
}
}
}
静态void Main(字符串[]参数)
{
IEnumerable products=GetProductsFromDatabase().ToList();
//这才是你真正想要的
products=(从products组product by product.Id中的product进入p select new product(){Id=p.Key,Name=p.select(i=>i.Name).ToCsv(),QUOTE=p.Sum(i=>i.QUOTE)});
}
}
}
这应该会让您非常接近(在.net 3.5中):
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间控制台应用程序1
{
公共类产品
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共整数数量{get;set;}
来自IDataReader的公共静态产品(System.Data.IDataReader阅读器)
{
产品=新产品();
product.Id=reader.GetInt32(reader.GetOrdinal(“ProductId”);
product.Name=reader.IsDBNull(reader.GetOrdinal(“ProductName”))?
string.Empty:reader.GetString(reader.GetOrdinal(“ProductName”);
product.Quantity=reader.IsDBNull(reader.GetOrdinal(“ProductQuantity”))?
0:reader.GetInt32(reader.GetOrdinal(“ProductQuantity”);
退货产品;
}
}
公共静态类LinqExtensions
{
公共静态字符串ToCsv(此IEnumerable items)
{
System.Text.StringBuilder sb=新的StringBuilder();
foreach(项中的字符串项)
{
某人附加(项目);
某人加上(“,”);
}
使某人返回字符串修剪(“”,’);
}
}
班级计划
{
静态IEnumerable GetProductsFromDatabase()
{
使用(System.Data.SqlClient.SqlConnection connection=new System.Data.SqlClient.SqlConnection(“某些连接字符串”))
{
字符串sql=@“返回productid、productname和productQuantity列的某些sql查询”;
使用(System.Data.SqlClient.SqlCommand=new System.Data.SqlClient.SqlCommand(sql,连接))
{
connection.Open();
使用(System.Data.IDataReader reader=command.ExecuteReader())
{
while(reader.Read())
从IDataReader(读卡器)返回的产品产量;
}
}
}
}
静态void Main(字符串[]参数)
{
IEnumerable products=GetProductsFromDatabase().ToList();
//这才是你真正想要的
products=(从products组product by product.Id中的product进入p select new product(){Id=p.Key,Name=p.select(i=>i.Name).ToCsv(),QUOTE=p.Sum(i=>i.QUOTE)});
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public int Quantity { get; set; }
public static Product FromIDataReader(System.Data.IDataReader reader)
{
Product product = new Product();
product.Id = reader.GetInt32(reader.GetOrdinal("ProductId"));
product.Name = reader.IsDBNull(reader.GetOrdinal("ProductName")) ?
string.Empty : reader.GetString(reader.GetOrdinal("ProductName"));
product.Quantity = reader.IsDBNull(reader.GetOrdinal("ProductQuantity")) ?
0 : reader.GetInt32(reader.GetOrdinal("ProductQuantity"));
return product;
}
}
public static class LinqExtensions
{
public static string ToCsv(this IEnumerable<string> items)
{
System.Text.StringBuilder sb = new StringBuilder();
foreach(string item in items)
{
sb.Append(item);
sb.Append(", ");
}
return sb.ToString().Trim(' ', ',');
}
}
class Program
{
static IEnumerable<Product> GetProductsFromDatabase()
{
using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection("Some Connection String"))
{
string sql = @"some sql query that returns productid, productname, and productQuantity columns";
using (System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(sql, connection))
{
connection.Open();
using (System.Data.IDataReader reader = command.ExecuteReader())
{
while (reader.Read())
yield return Product.FromIDataReader(reader);
}
}
}
}
static void Main(string[] args)
{
IEnumerable<Product> products = GetProductsFromDatabase().ToList();
// This is the bit you're really looking for
products = (from product in products group product by product.Id into p select new Product() { Id = p.Key, Name = p.Select(i => i.Name).ToCsv(), Quantity = p.Sum(i => i.Quantity) });
}
}
}