单个linq中的多个位置 [英] multiple wheres in a single linq

查看:71
本文介绍了单个linq中的多个位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在单个linq语句中执行多个where语句.下面的工作,但在3个单独的步骤中.可以一次完成吗?

I am trying to perform multiple where statements in a single linq statement. The following works, but in 3 separate steps. Can it be done in one?

var test = (from prop in typeof(Invoice).GetProperties()
            where prop.PropertyType == typeof (ItemInfo)
               from z in (from item in typeof (ItemInfo).GetProperties()
                          select new Prop{ Name = prop.Name + item.Name, 
                                           PropType = item.PropertyType})
               select z).ToList();
test.AddRange((from prop in typeof(Invoice).GetProperties()
               where prop.PropertyType == typeof(AmountInfo)
                  from z in (from amm in typeof(AmountInfo).GetProperties()
                             select new Prop{ Name = prop.Name + amm.Name, 
                                              PropType = amm.PropertyType })
                  select z).ToList());
test.AddRange((from prop in typeof(Invoice).GetProperties()
               where (prop.PropertyType != typeof(ItemInfo) && 
                      prop.PropertyType != typeof(AmountInfo))
               select new Prop { Name = prop.Name, 
                                 PropType = prop.PropertyType }));

推荐答案

由于2/3的查询有所不同(where和select子句),因此我认为三个查询除非您想放置一个在任何情况下都可以调用以执行选择部分的通用方法,否则它可能是正确的答案.但是由于您正在使用PropertyInfo,所以这实际上是不可能的.因此,我唯一要做的更改就是不要每次都调用GetProperties:

Since 2/3 of the queries varies (the where and select clauses), I think three queries is probably the right answer, unless you want to put a generic method that can be called to do the select part in any case. But since you''re using PropertyInfo that isn''t really possible. So the only thing I''d change is to not call GetProperties each time:

var props = typeof(Invoice).GetProperties();

var test = (from props
            where prop.PropertyType == typeof (ItemInfo)
               from z in (from item in typeof (ItemInfo).GetProperties()
                          select new Prop{ Name = prop.Name + item.Name, 
                                           PropType = item.PropertyType})
               select z).ToList();
test.AddRange((from props
               where prop.PropertyType == typeof(AmountInfo)
                  from z in (from amm in typeof(AmountInfo).GetProperties()
                             select new Prop{ Name = prop.Name + amm.Name, 
                                              PropType = amm.PropertyType })
                  select z).ToList());
test.AddRange((from props
               where (prop.PropertyType != typeof(ItemInfo) && 
                      prop.PropertyType != typeof(AmountInfo))
               select new Prop { Name = prop.Name, 
                                 PropType = prop.PropertyType }));



实际上,我可能根本不会在这里使用LINQ.



Actually, I probably wouldn''t use LINQ here at all.

List<Prop> test = new List<Prop>();
var props = typeof(Invoice).GetProperties();
foreach(PropertyInfo prop in props){
 if(prop.PropertyType == typeof(ItemInfo) || prop.PropertyType == typeof(AmountInfo))
  test.AddRange(typeof(prop.PropertyType).Properties.ForEach(p => new Prop { Name = prop.Name + p.Name, PropType = p.PropertyType});
 else
  test.Add(new Prop { Name = prop.Name, PropType = prop.PropertyType });
}



标准扩展名ForEach尚未在数组上定义,我想您可以像
那样进行



The standard extension ForEach isn''t defined on arrays, I think you can do it like

public static IEnumerable<U> ForEach<T, U>(this T[] values, Func<T,U> action){
 foreach(T t in values) yield return action(t);
}


为什么不:

Why not:

var test = (from prop in typeof(Invoice).GetProperties()
            where 
(prop.PropertyType == typeof (ItemInfo)) ||
(prop.PropertyType == typeof(AmountInfo)) || 
((prop.PropertyType != typeof(ItemInfo) &&
                      prop.PropertyType != typeof(AmountInfo)))
               from z in (from item in typeof (ItemInfo).GetProperties()
                          select new Prop{ Name = prop.Name + item.Name,
                                           PropType = item.PropertyType})


这篇关于单个linq中的多个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆