简体   繁体   中英

Multiple same item inside list

So, I think I have a problem with restrictions, or at least missing something. Basically I have a query that I execute with the following result set: 在此处输入图片说明

As you can see, there are 6 records. Now inside my application it will return 6 records but it will add the order OR00221 multiple times, which is expected but what isn't expected is that it will only add the first one, with freightnr 6 multiple times.

在此处输入图片说明

As you can see, it add the 3rd record from the resultset for all items with this OrdNr OR00221.

I believe the problem is somewhere here:

restriction.Add(Restrictions.In(Projections.Property<ItemToPlan>(x => x.TrailerType), TrailerType));

The source of the class:

   public static IList<ItemToPlan> GetItems(string[] TrailerType, Dictionary<string, string> filter, string orderBy, bool ascending, Dictionary<string, bool> groupingColumns)
        {
            if (TrailerType == null || TrailerType.Length == 0)
            {
                return new List<ItemToPlan>();
            }
            using (ISession session = NHibernateHelper.OpenSession())
            {
                var query = session.QueryOver<ItemToPlan>();
                var restriction = Restrictions.Conjunction();
                restriction.Add(Restrictions.In(Projections.Property<ItemToPlan>(x => x.TrailerType), TrailerType));
                if (filter != null)
                {
                    Type type = typeof(ItemToPlan);
                    IClassMetadata meta = session.SessionFactory.GetClassMetadata(type);

                    foreach (var item in filter)
                    {
                        if (!string.IsNullOrWhiteSpace(item.Value))
                        {
                            IType propertyType = meta.GetPropertyType(item.Key);

                            if (propertyType == NHibernateUtil.String)
                            {
                                restriction.Add(Restrictions.InsensitiveLike(Projections.Property(item.Key), item.Value, MatchMode.Anywhere));
                            }
                            else
                            {
                                restriction.Add(Restrictions.InsensitiveLike(Projections.Cast(NHibernateUtil.String, Projections.Property(item.Key)), item.Value, MatchMode.Anywhere));
                            }
                        }
                    }
                }
                if (restriction.ToString() != "()")
                {
                    query.Where(restriction);
                }
                if (groupingColumns != null)
                {
                    foreach (var item in groupingColumns)
                    {
                        if (item.Value)
                        {
                            query = query.OrderBy(Projections.Property(item.Key)).Asc;
                        }
                        else
                        {
                            query = query.OrderBy(Projections.Property(item.Key)).Desc;
                        }
                    }
                }
                else
                {
                    groupingColumns = new Dictionary<string, bool>();
                }
                if (!string.IsNullOrWhiteSpace(orderBy) && !groupingColumns.ContainsKey(orderBy))
                {
                    if (ascending)
                    {
                        query = query.OrderBy(Projections.Property(orderBy)).Asc;
                    }
                    else
                    {
                        query = query.OrderBy(Projections.Property(orderBy)).Desc;
                    }

                }
                var result = query.List<ItemToPlan>();

                return result;
            }

Source as pastebin: https://pastebin.com/aiTTBKBQ

You can use GroupBy to avoid duplication of records if this problem really exist in the query. I suppose that you have OrdNr property in your model.

Here is a sample of how to do this.

result = result.GroupBy(e => e.OrdNr).Select(e => e.FirstOrDefault());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM