我有一个与此类似的数据库表。
身份证| 状态| 类型
等等……等等
我正在使用 linq 尝试从这个集合中辨别不同的状态,就像这样
results = ctx.Status.Distinct(new StatusComparer()).ToList();
但这会返回所有状态,我使用以下链接构建下面的比较器,我在另一个 StackOverflow帖子中找到了该建议
public class StatusComparer : IEqualityComparer<Status>
{
public bool Equals(Status x, Status y)
{
// Check whether the compared objects reference the same data.
if (ReferenceEquals(x, y))
{
return true;
}
// Check whether any of the compared objects is null.
if (ReferenceEquals(x, null) || ReferenceEquals(y, null))
{
return false;
}
// Check whether the status' properties are equal.
return x.StatusDescription == y.StatusDescription && x.Type == y.Type && x.StatusID == y.StatusID;
}
public int GetHashCode(Status status)
{
// Get hash code for the Name field if it is not null.
var hashStatusId = status.StatusID.GetHashCode();
// Get hash code for the Code field.
var hashStatusDescription = status.StatusDescription.GetHashCode();
var hashStatusType = status.Type.GetHashCode();
// Calculate the hash code for the product.
return hashStatusId ^ hashStatusDescription ^ hashStatusType;
}
}
}
我的问题如下,早期我们有一个运行良好的系统,事实上他们想要另一个使用相同数据库的系统,所以我们对其进行了探测。搜索有一个高级选项,其中有几个过滤器,其中一个是状态,但是当你从上面可以看出(松散的)DB结构状态有不同的类型但相似的文本。我需要能够通过 Linq 通过不同的文本选择整个状态。所有帮助将不胜感激。
也试过
results = (from s in context.Status group s by s.StatusDescription into g select g.First()).ToList();
这也因 System.NotSupportedException 而失败