C# 实体框架4.1统计相同的数据库行 我的设置
我有两个类,这里显示的是本例所需的内容:C# 实体框架4.1统计相同的数据库行 我的设置,c#,asp.net-mvc-3,entity-framework,entity-relationship,C#,Asp.net Mvc 3,Entity Framework,Entity Relationship,我有两个类,这里显示的是本例所需的内容: public class Photo : Entity { [Key] public int Id { get; set; } public virtual ICollection<Tag> Tags { get; set; } public Photo() { Tags = new List<Tag>(); } } public class Tag : En
public class Photo : Entity
{
[Key]
public int Id { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
public Photo()
{
Tags = new List<Tag>();
}
}
public class Tag : Entity
{
[Key]
public string Text { get; set; }
public virtual ICollection<Photo> Photos { get; set; }
public Tag()
{
Photos = new List<Photo>();
}
}
EF/LINQ等效值是多少
第二个问题
如何获取每个标签的计数或50个最常用标签的计数?按标签计数:
from t in Context.Tags
select new
{
t.Text,
t.Photos.Count()
}
(from t in Context.Tags
let photoCount = t.Photos.Count()
orderby photoCount descending
select new
{
t.Text,
photoCount
}).FirstOrDefault()
(from t in Context.Tags
let photoCount = t.Photos.Count()
orderby photoCount descending
select new
{
t.Text,
photoCount
}).Take(50)
foreach(var result in Context.Tags.Select(x => new { t.Text, t.Photos.Count() }))
{
Console.WriteLine(string.Format("Text: {0}, Count: {1}", result.Text, result.Count.ToString());
}
最常用的标签:
from t in Context.Tags
select new
{
t.Text,
t.Photos.Count()
}
(from t in Context.Tags
let photoCount = t.Photos.Count()
orderby photoCount descending
select new
{
t.Text,
photoCount
}).FirstOrDefault()
(from t in Context.Tags
let photoCount = t.Photos.Count()
orderby photoCount descending
select new
{
t.Text,
photoCount
}).Take(50)
foreach(var result in Context.Tags.Select(x => new { t.Text, t.Photos.Count() }))
{
Console.WriteLine(string.Format("Text: {0}, Count: {1}", result.Text, result.Count.ToString());
}
50个最常用的标签(实际上可能没有50个):
from t in Context.Tags
select new
{
t.Text,
t.Photos.Count()
}
(from t in Context.Tags
let photoCount = t.Photos.Count()
orderby photoCount descending
select new
{
t.Text,
photoCount
}).FirstOrDefault()
(from t in Context.Tags
let photoCount = t.Photos.Count()
orderby photoCount descending
select new
{
t.Text,
photoCount
}).Take(50)
foreach(var result in Context.Tags.Select(x => new { t.Text, t.Photos.Count() }))
{
Console.WriteLine(string.Format("Text: {0}, Count: {1}", result.Text, result.Count.ToString());
}
写意,所以可能不是100%语法正确,也不是最小/最可读的方式
编辑:添加示例:
from t in Context.Tags
select new
{
t.Text,
t.Photos.Count()
}
(from t in Context.Tags
let photoCount = t.Photos.Count()
orderby photoCount descending
select new
{
t.Text,
photoCount
}).FirstOrDefault()
(from t in Context.Tags
let photoCount = t.Photos.Count()
orderby photoCount descending
select new
{
t.Text,
photoCount
}).Take(50)
foreach(var result in Context.Tags.Select(x => new { t.Text, t.Photos.Count() }))
{
Console.WriteLine(string.Format("Text: {0}, Count: {1}", result.Text, result.Count.ToString());
}
谢谢,看起来不错。你能用一个例子来进一步说明如何循环所有找到的标记及其计数吗?非常好!有没有更简单的方法只获取最低的标签计数和最高的标签计数?您只需要数字吗?或者你想要数字和标签文本吗?算出了:
intmincount=result.Min(x=>x.Count)代码>。