我的Web API控制器有一个方法,用于从数据库表中检索指定数量的描述。有不同ID的重复描述,所以有时查询会在使用SELECT TOP时返回重复项。我还添加了随机(ORDER BY NEWID
)以减少获得双重球员的机会,但有时重复仍会返回。我想将查询更改为SELECT DISTINCT,但不知道如何在我的特定情况下执行此操作。在这里使用First()似乎很复杂。谁能帮忙?我的方法是如下:将select distinct添加到linq查询
public List<String> GetRandomDescriptions(string cat, string subcat, int n)
{
using (MyContext ctx = new MyContext())
{
var temp = ctx.Interactions.Where(d => (d.Category.Equals(cat) && d.Subcategory.Equals(subcat)))).OrderBy(d=>Guid.NewGuid()).Take(n).Select(d=>d.Description).ToList();
return temp;
}
}
这里是我的类:
[Table("[Records]")]
public class Interaction
{
[Key, Column("RECORD_ID")]
public string DescId { get; set;}
public string Category { get; set; }
public string Subcategory { get; set; }
public string Description{get; set;}
}
“选择不同的” =='选择(...)。鲜明的()' –
你只有n项想要什么? –