开发者

How to use the distinct on databases DB4O

开发者 https://www.devze.com 2022-12-24 06:19 出处:网络
the data like this: file1file2 aaaamilk aaaared bbbbbox bbbbpen ccccrose i want get result like this: file1: aaaa

the data like this: file1 file2 aaaa milk aaaa red bbbb box bbbb pen cccc rose i want get result like this: file1: aaaa bbbb cccc who can tell me how to do using DB4objects 开发者_如何学Cwaiting online....


As far as I know db4o has no direct support for the 'distinct' operation. However since you're using the .NET-framework you can use the LINQ-Distinct operation on you result. I assume you're using .NET 3.5 and C#. Tell me when I'm wrong.

For example:

  IObjectContainer db = // ...
  var persons = (from Person p in db
       select p).Distinct();

This will return the distinct result of all Person-objects. It will use the GetHashCode() and Equals()-Methods to compare the objects.

When you don't want to use a the default equal comparison, you can pass an IEqualityOperator-instance to the distinct method:

    class PersonByNameEquality : IEqualityComparer<Person>
    {
        public bool Equals(Person x, Person y)
        {
            return x.Firstname.Equals(y.Firstname);
        }

        public int GetHashCode(Person obj)
        {
            return obj.Firstname.GetHashCode();
        }
    }

    // and then
    IObjectContainer db = //...
    var persons = (from Person p in db
                   select p).Distinct(new PersonByNameEquality());
0

精彩评论

暂无评论...
验证码 换一张
取 消