Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.Linq.Dynamic.Core:如何正确选择多个?_C#_.net_Linq_Dynamic Linq - Fatal编程技术网

C# System.Linq.Dynamic.Core:如何正确选择多个?

C# System.Linq.Dynamic.Core:如何正确选择多个?,c#,.net,linq,dynamic-linq,C#,.net,Linq,Dynamic Linq,我是这个系统的新手。Linq.Dynamic.Core。我有这个: var equipments= from pack in Packs where pack.Equipments.Any() select pack.Equipments; var secondEquipments = from pac in equipments where pac.GenericEquipment.Id == 2 select pac; //I could use one variable i

我是这个系统的新手。Linq.Dynamic.Core。我有这个:

var equipments= from pack in Packs where pack.Equipments.Any() select pack.Equipments;

var secondEquipments = from pac in equipments where       
pac.GenericEquipment.Id == 2 select pac;
//I could use one variable instead of 2 but that would look a little bit complex
假设我们有:

Packs = new List<Pack>
{
    new Pack()
    {
        IdAtSource="Pack1",
        Equipments= new List<Equipment>()
        {
            new Equipment
            {
                Id=1,
                GenericEquipment = new GenericEquipment()
                {
                    Id=7
                }
            }
        }
    },
    new Pack()
    {
        IdAtSource="Pack2",
        Equipments= new List<Equipment>()
        {
            new Equipment
            {
                Id=2,
                GenericEquipment = new GenericEquipment()
                {
                    Id=1
                }
            },
            new Equipment
            {
                Id=2,
                GenericEquipment = new GenericEquipment()
                {
                    Id=2
                }
            }
        }
    }
}
var equipments= from pack in Packs where pack.Equipments.Any() select pack.Equipments;

var secondEquipments = from pac in equipments where       
pac.GenericEquipment.Id == 2 select pac;
//I could use one variable instead of 2 but that would look a little bit complex
但我觉得我是这里的靶子。还有关于如何使用这个库的文档页吗

var equipments= from pack in Packs where pack.Equipments.Any() select pack.Equipments;

var secondEquipments = from pac in equipments where       
pac.GenericEquipment.Id == 2 select pac;
//I could use one variable instead of 2 but that would look a little bit complex

非常感谢

这些应该有用:

var equipments= from pack in Packs where pack.Equipments.Any() select pack.Equipments;

var secondEquipments = from pac in equipments where       
pac.GenericEquipment.Id == 2 select pac;
//I could use one variable instead of 2 but that would look a little bit complex
Msdn链接(很抱歉,我在手机上,无法重命名):
动态LINQ的最新版本似乎是该项目及其文档

var equipments= from pack in Packs where pack.Equipments.Any() select pack.Equipments;

var secondEquipments = from pac in equipments where       
pac.GenericEquipment.Id == 2 select pac;
//I could use one variable instead of 2 but that would look a little bit complex

通常,您需要动态LINQ而不是标准LINQ的唯一原因是当编译时类型未知时

var equipments= from pack in Packs where pack.Equipments.Any() select pack.Equipments;

var secondEquipments = from pac in equipments where       
pac.GenericEquipment.Id == 2 select pac;
//I could use one variable instead of 2 but that would look a little bit complex
如果您在编译时知道查询将针对
列表
,则可以使用stanadard LINQ,如下代码所示(请注意,这将修改
的原始实例):

var equipments= from pack in Packs where pack.Equipments.Any() select pack.Equipments;

var secondEquipments = from pac in equipments where       
pac.GenericEquipment.Id == 2 select pac;
//I could use one variable instead of 2 but that would look a little bit complex
如果需要不修改原始实例的代码,此代码将创建原始实例的副本:

var equipments= from pack in Packs where pack.Equipments.Any() select pack.Equipments;

var secondEquipments = from pac in equipments where       
pac.GenericEquipment.Id == 2 select pac;
//I could use one variable instead of 2 but that would look a little bit complex
var usefulPacks = Packs.Select(pack => {
    return new Pack() {
        IDAtSource = pack.IDAtSource,
        Equipments = pack.Equipments.Where(equipment => 
            equipment.GenericEquipment.Id == 1
        ).ToList(); 
    };
}).Where(pack => pack.Equipments.Any()).ToList();
请注意,这不是使用
。SelectMany
-
。SelectMany
用于从嵌套的枚举创建单个枚举;这里,最终列表中的每个
Pack
对应于原始列表中的一个
Pack

var equipments= from pack in Packs where pack.Equipments.Any() select pack.Equipments;

var secondEquipments = from pac in equipments where       
pac.GenericEquipment.Id == 2 select pac;
//I could use one variable instead of 2 but that would look a little bit complex

动态LINQ不支持在表达式中修改或初始化属性:

var equipments= from pack in Packs where pack.Equipments.Any() select pack.Equipments;

var secondEquipments = from pac in equipments where       
pac.GenericEquipment.Id == 2 select pac;
//I could use one variable instead of 2 but that would look a little bit complex
表达式语言允许获取(但不设置)任何可访问的公共字段、属性或索引器的值

var equipments= from pack in Packs where pack.Equipments.Any() select pack.Equipments;

var secondEquipments = from pac in equipments where       
pac.GenericEquipment.Id == 2 select pac;
//I could use one variable instead of 2 but that would look a little bit complex
因此,
设备
属性不能更改/初始化为仅包含符合条件的
设备
实例

var equipments= from pack in Packs where pack.Equipments.Any() select pack.Equipments;

var secondEquipments = from pac in equipments where       
pac.GenericEquipment.Id == 2 select pac;
//I could use one variable instead of 2 but that would look a little bit complex
要设置
设备
,您有两种选择:

var equipments= from pack in Packs where pack.Equipments.Any() select pack.Equipments;

var secondEquipments = from pac in equipments where       
pac.GenericEquipment.Id == 2 select pac;
//I could use one variable instead of 2 but that would look a little bit complex
  • 将构造函数添加到
    Pack
    ,该构造函数采用适当的参数
  • 在任何具有适当参数的类上编写静态方法

  • 将构造函数添加到
    Pack

    var equipments= from pack in Packs where pack.Equipments.Any() select pack.Equipments;
    
    var secondEquipments = from pac in equipments where       
    pac.GenericEquipment.Id == 2 select pac;
    //I could use one variable instead of 2 but that would look a little bit complex
    
    您可以使用适当的参数将构造函数添加到
    Pack
    ,以设置
    设备

    var equipments= from pack in Packs where pack.Equipments.Any() select pack.Equipments;
    
    var secondEquipments = from pac in equipments where       
    pac.GenericEquipment.Id == 2 select pac;
    //I could use one variable instead of 2 but that would look a little bit complex
    
    Pack(int IDAtSource, IEnumerable<Equipment> equipments) {
        this.IDAtSource = IDAtSource;
        this.Equipments = equipments.ToList();
    }
    

    定义静态方法

    var equipments= from pack in Packs where pack.Equipments.Any() select pack.Equipments;
    
    var secondEquipments = from pac in equipments where       
    pac.GenericEquipment.Id == 2 select pac;
    //I could use one variable instead of 2 but that would look a little bit complex
    
    public static class MyPackMethods {
        public static Pack Create(int IDAtSource, IEnumerable<Equipment> equipments) {
            return new Pack() {
                IDAtSource = IDAtSource,
                Equipments = equipments.ToList()
            };
        }
    }
    

    谢谢你,好心的先生!但是这并没有回答我的问题,关于如何使用库Linq.Dynamic.Core。没问题,但我认为这回答了你的问题。请让我更正一下:)非常感谢你的回答!事实上,它几乎回答了我的问题。当我完成选择时,我需要有一个包,只有那些具有Id=1的通用设备的设备。在设备清单中,只有一台设备应显示。而且,我不知道运行时的类型,这只是一个简单的例子。再次感谢,您的asnwer非常有帮助。@这就是动态LINQ查询中发生的事情。首先,我们调用
    。选择
    ,这将为
    Pack
    的每个初始实例创建一个
    Pack
    的新实例,但只将匹配的
    设备
    传递给新实例的构造函数:
    设备。其中(GenericEquipment.ID=1)
    ;然后我们调用
    .Where
    ,只包括
    的实例,这些实例在
    设备
    属性中有一些内容。明白了,亲爱的先生。但不幸的是,这不是一个改变构造函数的选项,所以我可以使用这个解决方案。