1。类public class TestClass
{
private string id;
private string name;
private string score;
private string Date;
//get set 方法
}2。集合TestAList<TestClass>
此集合是 score 分数大于80分的集合
3。集合TestBList<TestClass>
此集合是 Date  日期在 2009 到 2011的集合
4。声明 NewList<TestClass>集合
想用Linq 把两集合 合并成一个 并且值重复 返回结果为分数大于80分 日期在2009 到 2011的集合,然后放到新的NewList<TestClass>中
期待 linq 大神

解决方案 »

  1.   

    如果两个集合中都存在 
    分数大于80分 日期在2009 到 2011的同一ID的 TestClass 怎么取?
      

  2.   

    A集合中: new TestClass{ Id=1, Name="A", Score="100", Date="1/1/2008"},B集合中: new TestClass{ Id=1, Name="A", Score="70", Date="1/1/2010"},合并后舍弃哪条?
      

  3.   

    还是你想把两条合并作为返回结果? new TestClass{ Id=1, Name="A", Score="100", Date="1/1/2010"},
      

  4.   

    List<TestClass> newlist = TestAList.Where(x => DateTime.Parse(x.Date).Year >= 2009 && DateTime.Parse(x.Date).Year <= 2011).Union(TestBList.Where(x => Convert.ToInt32(x.Score) >= 80));
      

  5.   


    此集合存的值为
    TestAList<TestClass> 
    {
    new TestClass(){id="1",name="a",score="88"}
    ,new TestClass(){id="2",name="b",score="85"}
    ,new TestClass(){id="3",name="c",score="92"}
    ,new TestClass(){id="4",name="d",score="99"}
    ,new TestClass(){id="5",name="e",score="93"}
    ,new TestClass(){id="6",name="f",score="91"}}
    此集合存的值为
    TestBList<TestClass>
    {
    new TestClass(){id="1",name="a",date="2009-07-12"}
    ,new TestClass(){id="2",name="b",date="2009-08-02"}
    ,new TestClass(){id="3",name="c",date="2010-11-05"}
    ,new TestClass(){id="6",name="f",date="2009-09-02"}
    }
    这里 TestAList<TestClass> 中的集合 id name  与 TestBList<TestClass> 的id name 值是一样的  只不过 TestBList<TestClass>中有 date
    我想返回 一个新的NewList<TestClass> 此集合包括
    id name score date 这些值都是有值的
    也就是说NewList<TestClass> 要了TestAList<TestClass>的 id name 和 score 要了
    TestBList<TestClass>的 date
      

  6.   


    void Main()
    {
    var TestAList=new List<TestClass>
    {
      new TestClass{ Id=1, Name="A", Score="100", Date="1/1/2008"},
      new TestClass{ Id=2, Name="B", Score="88", Date="1/2/2007"},
      new TestClass{ Id=3, Name="C", Score="99", Date="1/4/2006"},
      new TestClass{ Id=4, Name="D", Score="85", Date="1/5/2007"}
    };

    var TestBList=new List<TestClass>
    {
      new TestClass{ Id=1, Name="A", Score="77", Date="1/4/2009"},
      new TestClass{ Id=2, Name="B", Score="70", Date="1/11/2009"},
      new TestClass{ Id=3, Name="C", Score="66", Date="1/5/2010"},
      new TestClass{ Id=4, Name="D", Score="55", Date="1/6/2011"}
    };
    var NewList=(from t in TestAList.Concat(TestBList)
                group t by t.Id into g  
    select new TestClass
    {
     Id=g.Key,
      Name=g.First().Name,
      Score=g.OrderByDescending(m=>Convert.ToInt32(m.Score)).First().Score,
      Date=g.OrderByDescending(m=>Convert.ToDateTime(m.Date)).First().Date
    }).ToList();
    Console.WriteLine(NewList);
    }
    public class TestClass
    {
    public int Id{get;set;}
    public string Name{get;set;}
    public string Score{get;set;}
    public string Date{get;set;}}
      

  7.   


    看来被我猜对 了
    不过我对你的类定义有些建议:
    public class TestClass
    {
    public int Id{get;set;}
    public string Name{get;set;}
    public int Score{get;set;}
    public DateTime Date{get;set;}}如果你这样定义的话  那么LINQ查询中就没必要先排序再取值了
    直接这样即可:
    var NewList=(from t in TestAList.Concat(TestBList)
                group t by t.Id into g  
    select new TestClass
    {
     Id=g.Key,
      Name=g.First().Name,
      Score=g.Max(m=>m.Score),
      Date=g.Max(m=>m.Date)
    }).ToList();
      

  8.   

    TestAList<TestClass> newList= TestALists.FindAll(c=>c.Core>80&&(time>xx&&time<xx))
      

  9.   

    囧~~ 也就是LINQ相关的帖子能让我想静下心来看看