我有一个如下所示的存储过程:
SELECT UrlId, TypeId, DomainId, Url, d.OrgId AS orgId
FROM SystemUrls
JOIN Domaindata d ON d.Id = DomainId
它给了我这个结果:
代码中的看起来像:
我想要的是什么:
是将此结果分组到domainId上 所以我得到两行。
我有这堂课:
public class TestModel
{
public long DomainId { get; set; }
public List<SystemUrl> Urls { get; set; }
}
我想得到一个结果:
DomainId 79 Urls.count()= 2
DomainId 81 Urls.COunt = 2
我的尝试:
var t =
(from u in urls
select
new TestModel
{
DomainId = u.DomainId,
Urls = new List<SystemUrl> {new SystemUrl {Url = u.Url}}
}).GroupBy(v => v.DomainId).ToList();
问题是我似乎得到了domainId,但似乎没有其他数据:
我该如何解决这个问题?
答案 0 :(得分:2)
var t = urls.GroupBy(u => u.DomainId)
.Select(g => new TestModel{
DomainId = g.Key,
Urls = g.Select(u => new SystemUrl {Url = u.Url}).ToList()
})
.ToList();
答案 1 :(得分:2)
以下是如何做到这一点:
var t = urls.GroupBy(v => v.DomainId).Select(g => new TestModel
{
DomainId = g.Key,
Urls = g.Select(u => new SystemUrl {Url = u.Url}).ToList()
}).ToList();
答案 2 :(得分:0)
将DataAdapter结果放入数据表中。然后使用以下内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
namespace ConsoleApplication49
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("Urld", typeof(int));
dt.Columns.Add("Typeid", typeof(int));
dt.Columns.Add("DomainId", typeof(int));
dt.Rows.Add(new object[] { 13, 1, 79 });
dt.Rows.Add(new object[] { 14, 2, 79 });
dt.Rows.Add(new object[] { 15, 2, 81 });
dt.Rows.Add(new object[] { 16, 1, 81 });
var results = dt.AsEnumerable()
.GroupBy(x => x.Field<int>("DomainID"))
.ToDictionary(x => x.Key, y => y.Count());
}
}
}