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
C# 列表中的Linq列表_C#_Linq_List - Fatal编程技术网

C# 列表中的Linq列表

C# 列表中的Linq列表,c#,linq,list,C#,Linq,List,我在一个查询中合并两个列表时遇到问题: public class Class1 { public int id { get; set; } public List<Class2> attr { get; set; } } public class Class2 { public int id { get; set; } } 这个问题有什么解决办法吗 问题: 我的问题是我的结果总是空的。如果我删除第二个查询 attr

我在一个查询中合并两个列表时遇到问题:

public class Class1 {
    public int id { get; set; }
    public List<Class2> attr { get; set; }
}

public class Class2 {
    public int id { get; set; }
}
这个问题有什么解决办法吗

问题: 我的问题是我的结果总是空的。如果我删除第二个查询

                     attr = (from t in context.table2
                                 where m.id == t.id
                                 select new Class2 { 
                                    id= t.id
                                 }).Take(5).ToList()

,我的查询有效

您可以尝试以下方法:-

 var listAB = listA.Cast<object>().Union(listB.Cast<object>()).ToLookup(x => x is TypeA ? (x as TypeA).Key : (x as TypeB).Key)
            .Select(kv => {
                var a = kv.FirstOrDefault(x => x is TypeA) as TypeA;
                var b = kv.FirstOrDefault(x => x is TypeB) as TypeB;
                return new TypeAB() {
                    Key = kv.Key,
                    ValueA = a != null ? (int?)a.Value : null,
                    ValueB = b != null ? (int?)b.Value : null
                };
            }).ToList();
var listAB=listA.Cast().Union(listB.Cast()).ToLookup(x=>x为TypeA?(x为TypeA).Key:(x为TypeB.Key)
.选择(千伏=>{
var a=kv。第一个默认值(x=>x为TypeA)为TypeA;
var b=kv。第一个默认值(x=>x为类型b)为类型b;
返回新的TypeAB(){
键=千伏键,
ValueA=a!=null?(int?)a.Value:null,
ValueB=b!=null?(int?)b.值:null
};
}).ToList();

下面的代码工作得很好(我用自定义变量替换了
上下文变量),因此,我认为,您最好检查
where
语句

using System.Linq;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class Class1
    {
        public int id { get; set; }
        public List<Class2> attr { get; set; }
    }

    public class Class2
    {
        public int id { get; set; }
    }

    class MyEntity
    {
        public int ID { get; set; }
        public MyEntity(int id)
        {
            ID = id;
        }
    }
    class MyContext : List<MyEntity>
    {
    }

    class Program
    {
        static void Main(string[] args)
        {

            var context = new MyContext();
            context.Add(new MyEntity(1));
            context.Add(new MyEntity(2));
            context.Add(new MyEntity(3));
            context.Add(new MyEntity(4));
            context.Add(new MyEntity(5));

            var q = (from m in context
                     select new Class1
                     {
                         id = m.ID,
                         attr = (from t in context
                                 where m.ID == t.ID
                                 select new Class2
                                 {
                                     id = t.ID
                                 }).Take(5).ToList()
                     }).Take(1).ToList();
        }
    }
}
使用System.Linq;
使用System.Collections.Generic;
命名空间控制台应用程序1
{
公共班级1
{
公共int id{get;set;}
公共列表属性{get;set;}
}
公共课2
{
公共int id{get;set;}
}
类实体
{
公共int ID{get;set;}
公共MyEntity(内部id)
{
ID=ID;
}
}
类MyContext:List
{
}
班级计划
{
静态void Main(字符串[]参数)
{
var context=new MyContext();
添加(新MyEntity(1));
添加(新MyEntity(2));
添加(新MyEntity(3));
添加(新MyEntity(4));
添加(新MyEntity(5));
var q=(从上下文中的m开始)
选择新类别1
{
id=m.id,
attr=(从上下文中的t开始)
其中m.ID==t.ID
选择新类别2
{
id=t.id
}).采取(5)措施
}).Take(1.ToList();
}
}
}

我找到了灵魂。我没有使用列表,而是将它改为IQueryable,现在它可以工作了

我记得我的错误消息:“linq to entities无法识别方法列表…linq此方法无法转换为存储表达式”


有人明白吗?

有什么问题吗?有错误信息吗?很抱歉我的问题。我是这个论坛的新成员。@Calimero:你可以编辑你的问题来添加问题,然后人们会撤销他们的反对票,你会得到更好的回应。顺便说一句,正如John提到的Stackoverflow不是一个普通的论坛,请参阅。问题是,查询不起作用,我的结果总是空的。我是这个网站的新手。令人尴尬的
using System.Linq;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class Class1
    {
        public int id { get; set; }
        public List<Class2> attr { get; set; }
    }

    public class Class2
    {
        public int id { get; set; }
    }

    class MyEntity
    {
        public int ID { get; set; }
        public MyEntity(int id)
        {
            ID = id;
        }
    }
    class MyContext : List<MyEntity>
    {
    }

    class Program
    {
        static void Main(string[] args)
        {

            var context = new MyContext();
            context.Add(new MyEntity(1));
            context.Add(new MyEntity(2));
            context.Add(new MyEntity(3));
            context.Add(new MyEntity(4));
            context.Add(new MyEntity(5));

            var q = (from m in context
                     select new Class1
                     {
                         id = m.ID,
                         attr = (from t in context
                                 where m.ID == t.ID
                                 select new Class2
                                 {
                                     id = t.ID
                                 }).Take(5).ToList()
                     }).Take(1).ToList();
        }
    }
}