Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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
Linq 从IEnumerable返回数据_Linq_C# 4.0_Collections_Lambda - Fatal编程技术网

Linq 从IEnumerable返回数据

Linq 从IEnumerable返回数据,linq,c#-4.0,collections,lambda,Linq,C# 4.0,Collections,Lambda,考虑一下 public class Obj { public string PropertyName; public string Name; public int Id; public int Value; } List<Obj> lsObjdata = new List<Obj>(); var obj = new Obj { Name = "xyz", PropertyName = "Volume", Id= 1, Valu

考虑一下

public class Obj
{
     public string PropertyName;
     public string Name;
     public int Id;
     public int Value;
}

List<Obj> lsObjdata = new List<Obj>();
var obj  = new Obj { Name = "xyz", PropertyName = "Volume", Id= 1, Value = 25};
lsObjdata.Add(obj);
obj  = new Obj { Name = "abc", PropertyName = "Volume", Id= 1, Value = 23};
lsObjdata.Add(obj);
obj = new Obj {Name = "abc", PropertyName = "Oil", Id =1, Value = 45};
lsObjdata.Add(obj);

IEnumerable<IGrouping<String, obj>> results = lsObjData.GroupBy( m => m.Id);
此属性和值不存在,名称为
xyz
。这应该添加到我的最终输出中。我的结果应该包含列表中的以下两个对象

obj = new {Name = "abc", PropertyName = "Oil", Id =1, Value = 45}
obj  = new { Name = "xyz", PropertyName = "Volume", Id= 1, Value = 25};
PS:我已经完成了解决方案。 但是,我无法使用Linq/Lambda表达式并在一行/一笔中获得解决方案。 有人能帮我吗? 谢谢
Sam

您需要按两个属性进行分组-
Id,PropertyName
,然后通过如下方式对分组数据排序来选择第一个对象:-

var results = lsobjdata.GroupBy(x => new { x.Id, x.PropertyName })
                        .Select(x => x.OrderByDescending(z => z.Name).FirstOrDefault());

下面是一些示例数据。

因为您没有提到您想要每个结果集的第一个。我给出了另一个解决方案

// Define other methods and classes here
public class Obj
{
    public string PropertyName;
    public string Name;
    public int Id;
    public int Value;
}

public class Key
{
    public string PropertyName;
    public int Id;

    public override bool Equals(object obj)
    {
    Key item = obj as Key;

        return item.Id == this.Id && item.PropertyName == this.PropertyName;
    }

    public override int GetHashCode()
    {
        int hash = 13;
        hash = (hash * 7) + Id.GetHashCode();
        hash = (hash * 7) + PropertyName.GetHashCode();
        return hash;
    }
}

void static Main()
{
    List<Obj> lsObjdata = new List<Obj>();
    HashSet<Key>  keys = new HashSet<Key>();
    var obj  = new Obj { Name = "xyz", PropertyName = "Volume", Id= 1, Value = 25};
    lsObjdata.Add(obj);
    obj  = new Obj { Name = "abc", PropertyName = "Volume", Id= 1, Value = 23};
    lsObjdata.Add(obj);
    obj = new Obj {Name = "abc", PropertyName = "Oil", Id =1, Value = 45};
    lsObjdata.Add(obj);
    obj = new Obj {Name = "abc", PropertyName = "Gas", Id =1, Value = 45};
    lsObjdata.Add(obj);
    obj = new Obj {Name = "edf", PropertyName = "Gas", Id =1, Value = 45};
    lsObjdata.Add(obj);
    var results = lsObjdata.GroupBy(m => new Key { Id = m.Id, PropertyName = m.PropertyName })
                                .Select<IGrouping<Key,Obj>,IEnumerable<Obj>>(x =>
                                {
                                    if (x.Any(v => v.Name == "xyz") && !keys.Contains(x.Key))
                                    {
                                            return new Obj[]{x.First(v => v.Name == "xyz")};
                                    }
                                    else if (!keys.Contains(x.Key as Key))
                                    {
                                            return x.Select(v=>v);                                      
                                    }
                                    else
                                        return null;
                                })
                                .SelectMany(x=>x)
                                .Where(x=> x != null);

    foreach (var res in results)
    {

            Console.WriteLine(res.PropertyName + "  "+res.Name+"  "+res.Id+"  "+ res.Value);
    }

    Console.WriteLine(results);
}
//在此处定义其他方法和类
公共类Obj
{
公共字符串PropertyName;
公共字符串名称;
公共int Id;
公共价值观;
}
公开类密钥
{
公共字符串PropertyName;
公共int Id;
公共覆盖布尔等于(对象对象对象)
{
关键项目=作为关键的obj;
return item.Id==this.Id&&item.PropertyName==this.PropertyName;
}
公共覆盖int GetHashCode()
{
int hash=13;
hash=(hash*7)+Id.GetHashCode();
hash=(hash*7)+PropertyName.GetHashCode();
返回散列;
}
}
void static Main()
{
List lsObjdata=新列表();
HashSet keys=新的HashSet();
var obj=new obj{Name=“xyz”,PropertyName=“Volume”,Id=1,Value=25};
lsObjdata.Add(obj);
obj=newobj{Name=“abc”,PropertyName=“Volume”,Id=1,Value=23};
lsObjdata.Add(obj);
obj=newobj{Name=“abc”,PropertyName=“Oil”,Id=1,Value=45};
lsObjdata.Add(obj);
obj=newobj{Name=“abc”,PropertyName=“Gas”,Id=1,Value=45};
lsObjdata.Add(obj);
obj=newobj{Name=“edf”,PropertyName=“Gas”,Id=1,Value=45};
lsObjdata.Add(obj);
var results=lsObjdata.GroupBy(m=>newkey{Id=m.Id,PropertyName=m.PropertyName})
.选择(x=>
{
if(x.Any(v=>v.Name==“xyz”)&&&!keys.Contains(x.Key))
{
返回新的Obj[]{x.First(v=>v.Name==“xyz”)};
}
如果(!keys.Contains(x.Key作为键))
{
返回x.Select(v=>v);
}
其他的
返回null;
})
.SelectMany(x=>x)
。其中(x=>x!=null);
foreach(结果中的var res)
{
Console.WriteLine(res.PropertyName+“”+res.Name+“”+res.Id+“”+res.Value);
}
控制台写入线(结果);
}

这将用哪种语言编译?您需要在两个属性
ID
PropertyName
上添加
GroupBy
。这会给你一个开始。@stefankmitph这就是逻辑。谁告诉我它会编译anywhere mate?@sam标签c#-4.0告诉我。。。别生气,伙计!
// Define other methods and classes here
public class Obj
{
    public string PropertyName;
    public string Name;
    public int Id;
    public int Value;
}

public class Key
{
    public string PropertyName;
    public int Id;

    public override bool Equals(object obj)
    {
    Key item = obj as Key;

        return item.Id == this.Id && item.PropertyName == this.PropertyName;
    }

    public override int GetHashCode()
    {
        int hash = 13;
        hash = (hash * 7) + Id.GetHashCode();
        hash = (hash * 7) + PropertyName.GetHashCode();
        return hash;
    }
}

void static Main()
{
    List<Obj> lsObjdata = new List<Obj>();
    HashSet<Key>  keys = new HashSet<Key>();
    var obj  = new Obj { Name = "xyz", PropertyName = "Volume", Id= 1, Value = 25};
    lsObjdata.Add(obj);
    obj  = new Obj { Name = "abc", PropertyName = "Volume", Id= 1, Value = 23};
    lsObjdata.Add(obj);
    obj = new Obj {Name = "abc", PropertyName = "Oil", Id =1, Value = 45};
    lsObjdata.Add(obj);
    obj = new Obj {Name = "abc", PropertyName = "Gas", Id =1, Value = 45};
    lsObjdata.Add(obj);
    obj = new Obj {Name = "edf", PropertyName = "Gas", Id =1, Value = 45};
    lsObjdata.Add(obj);
    var results = lsObjdata.GroupBy(m => new Key { Id = m.Id, PropertyName = m.PropertyName })
                                .Select<IGrouping<Key,Obj>,IEnumerable<Obj>>(x =>
                                {
                                    if (x.Any(v => v.Name == "xyz") && !keys.Contains(x.Key))
                                    {
                                            return new Obj[]{x.First(v => v.Name == "xyz")};
                                    }
                                    else if (!keys.Contains(x.Key as Key))
                                    {
                                            return x.Select(v=>v);                                      
                                    }
                                    else
                                        return null;
                                })
                                .SelectMany(x=>x)
                                .Where(x=> x != null);

    foreach (var res in results)
    {

            Console.WriteLine(res.PropertyName + "  "+res.Name+"  "+res.Id+"  "+ res.Value);
    }

    Console.WriteLine(results);
}