Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.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# 这群人怎么了_C#_Linq To Sql - Fatal编程技术网

C# 这群人怎么了

C# 这群人怎么了,c#,linq-to-sql,C#,Linq To Sql,我有一个Profile类,它有一个自引用自身的父属性,我想在一个sql join语句中加载一个配置文件及其父配置文件 Select * FROM profile left join profile as father on profile.fatherid = father.id where profile.id = 650 因此,我创建了以下linq语句,但不是运行关于它的sql语句,而是运行以下语句 select * from profiles select * from profile

我有一个Profile类,它有一个自引用自身的父属性,我想在一个sql join语句中加载一个配置文件及其父配置文件

Select * FROM profile left join profile as father on profile.fatherid = father.id where profile.id = 650
因此,我创建了以下linq语句,但不是运行关于它的sql语句,而是运行以下语句

select * from profiles

select * from profiles where id = 650
然后在内存中将它们分组在一起,但显然我不想加载整个数据库

    private class Result
    {
        public Profile Profile { get; set; }
        public IEnumerable<Profile> Fathers { get; set; }
    }
    private Result MapFather(Profile p, IEnumerable<Profile> father)
    {
        return new Result() {Profile = p, Fathers = father.DefaultIfEmpty()};
    }

   var profiles = from p in db.Profiles where p.ID.Equals(650) select p;
   var fathers = from f in db.Profiles select f;
   var groupJoin = profiles.GroupJoin(fathers,
                                        p => p.FatherID, 
                                        f => f.ID, 
                                        MapFather).ToList();
私有类结果
{
公共配置文件{get;set;}
公共IEnumerable父对象{get;set;}
}
私有结果映射父(配置文件p,IEnumerable父)
{
返回新结果(){Profile=p,Fathers=father.DefaultIfEmpty()};
}
var profiles=数据库中的p.profiles,其中p.ID.等于(650)选择p;
var fathers=数据库中的f。配置文件选择f;
var groupJoin=profiles.groupJoin(父亲、,
p=>p.FatherID,
f=>f.ID,
地图父亲);
对GroupJoin()的调用将获得IEnumerable版本,因为对MapFather的调用是Func而不是Expression>。因此,对配置文件和父文件的调用在GroupJoin之前执行

尝试内联MapFather():


您的语句可能太复杂,LINQ无法将其解析为单个SQL语句。你能用一个C#LINQ语句来写你的GroupJoin吗?如果我对LINQ不是很熟练,请原谅,但这不是获取整个表吗?您正在获取ID,然后在另一个字段的同一个表中加入组(与所有Records进行比较)……作为一个表达式,我将如何执行此操作?
var groupJoin = profiles.GroupJoin(fathers,
                                    p => p.FatherID, 
                                    f => f.ID, 
                                    (p, fathers) => new Result() {Profile = p, Fathers = father.DefaultIfEmpty()}).ToList();