我收到一个实体框架类型列表,并希望只返回List中的不同值.我正在使用以下方法,但它并没有统一列表.有什么建议?
帕拉姆: List<Flag> flags
List<Flag> distinctFlags = flags.Distinct().ToList();
Run Code Online (Sandbox Code Playgroud)
Flag的值如下:ID,Flag,FlagValue.我可以在这个例子中使用linq吗?
谢谢.
Cra*_*aig 17
假设Flag您的实体模型之一,您可以使用partial class和覆盖Equals和GetHashCode.这也假设您拥有一个Id属性,Flag class它具有唯一的身份.
//this namespace MUST match the namespace of your entity model.
namespace Your.Entity.Model.Namespace
{
public partial class Flag
{
public override bool Equals(object obj)
{
var item = obj as Flag;
if (item == null)
{
return false;
}
return this.Id.Equals(item.Id);
}
public override int GetHashCode()
{
return this.Id.GetHashCode();
}
}
}
Run Code Online (Sandbox Code Playgroud)
用法看起来像这样
List<Flag> distinctFlags = allFlags.Distinct().ToList();
Run Code Online (Sandbox Code Playgroud)