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
如何解决linq左连接查询中obejct引用不是对象实例的问题?_Linq - Fatal编程技术网

如何解决linq左连接查询中obejct引用不是对象实例的问题?

如何解决linq左连接查询中obejct引用不是对象实例的问题?,linq,Linq,以下是我的SQL查询: select AU.Last_Name From t_Batch B left join (select distinct customer_id, subbatchtypeid from t_allocationconfig where isactive='Y') AA on AA.customer_id = B.Customer_id and AA.SubBatchtypeid = B.SubBatchtypeid left join t_batcha

以下是我的SQL查询:

select  AU.Last_Name       
From t_Batch B
left join (select distinct customer_id, subbatchtypeid
from t_allocationconfig where isactive='Y')
AA on AA.customer_id = B.Customer_id and
AA.SubBatchtypeid = B.SubBatchtypeid
left join t_batchallocation BA on BA.Batchid = B.Batchid 
left join VW_users AU on AU.User_ID = BA.User_id;
我想将此SQL转换为Linq,但出现错误:

用户ID obecjt引用不是对象的实例

我如何解决这个问题

 IList<Batch> result = new List<Batch>();
 var batch = _batchservice.GetBatch().Where(r => r.ISACTIVE == "Y");
 var batchallocation = _batchallocationservice.GetBatchAllocation();
   var allocationconfig=_allocationconfigservice.GetAllocationConfig().Where(r=>r.ISACTIVE=="Y");
  result = (from  B in batch                          
                  join ac in (
                                  from cl in allocationconfig  where cl.ISACTIVE=="Y"
                                  select new {cl.CUSTOMER_ID,cl.SUBBATCHTYPEID}
                          ) on new  {B.CUSTOMER_ID,B.SUBBATCHTYPEID} 
                          equals new {ac.CUSTOMER_ID,ac.SUBBATCHTYPEID}  into aac 
                          from v in aac.DefaultIfEmpty(null)

                          join BA in batchallocation on B.BATCHID equals BA.BATCHID into x 
                          from y in x.DefaultIfEmpty()  

                          join AU in users on  y.USER_ID equals AU.USER_ID  into g 
                          from h in g.DefaultIfEmpty() 

                          select new Batch 
                          {
                           AllocatedUserName=h.LAST_NAME                                
                          }).ToList();
IList结果=新列表();
var batch=_batchservice.GetBatch()。其中(r=>r.ISACTIVE==“Y”);
var batchallocation=\u batchallocationservice.GetBatchAllocation();
var allocationconfig=_allocationconfigservice.GetAllocationConfig()。其中(r=>r.ISACTIVE==“Y”);
结果=(来自批次中的B)
加入ac(
从allocationconfig中的cl开始,其中cl.ISACTIVE==“Y”
选择新{cl.CUSTOMER\u ID,cl.SUBBATCHTYPEID}
)关于新的{B.CUSTOMER_ID,B.SUBBATCHTYPEID}
等于aac中的新{ac.CUSTOMER_ID,ac.SUBBATCHTYPEID}
从aac.DefaultIfEmpty中的v开始(null)
在B上的batchallocation中加入BA。BATCHID等于BA.BATCHID到x中
从x.DefaultIfEmpty()中的y开始
在y上加入AU-in用户。USER\u ID等于AU.USER\u ID进入g
从g.DefaultIfEmpty()中的h开始
选择新批次
{
AllocatedUserName=h.LAST\u NAME
}).ToList();

在使用y之前,您需要检查y是否为null

我猜问题在于x.DefaultIfEmpty返回一个null值,当连接访问到users表(y.USER\u ID)时,该值会导致错误

DefaultIfEmpty子句上有一个重载,它接受默认类型,例如

x.DefaultIfEmpty<BatchAllocation>(new BatchAllocation () { USER_ID = "-1", .... })
x.DefaultIfEmpty(新的批分配(){USER_ID=“-1”,…})
您可以尝试返回一个用户ID属性为-1(假设没有用户ID可以为-1)的虚拟批分配默认值,然后将h返回为null,然后您可以通过主查询中的where过滤掉null。(其中h!=null&&h.LAST_NAME!=null)

以下例子-

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

        LeftOuterJoinExample();

        Console.ReadLine();

    }

    class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    class Pet
    {
        public string Name { get; set; }
        public Person Owner { get; set; }
    }

    public static void LeftOuterJoinExample()
    {
        Person magnus = new Person { FirstName = "Magnus", LastName = "Hedlund" };
        Person terry = new Person { FirstName = "Terry", LastName = "Adams" };
        Person charlotte = new Person { FirstName = "Charlotte", LastName = "Weiss" };
        Person arlene = new Person { FirstName = "Arlene", LastName = "Huff" };

        Pet barley = new Pet { Name = "Barley", Owner = terry };
        Pet boots = new Pet { Name = "Boots", Owner = terry };
        Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte };
        Pet bluemoon = new Pet { Name = "Blue Moon", Owner = terry };
        Pet daisy = new Pet { Name = "Daisy", Owner = magnus };

        // Create two lists.
        List<Person> people = new List<Person> { magnus, terry, charlotte, arlene };
        List<Pet> pets = new List<Pet> { barley, boots, whiskers, bluemoon, daisy };

        var query = from person in people
                    join pet in pets on person equals pet.Owner into gj
                    from subpet in gj.DefaultIfEmpty<Pet>(new Pet() { Name = "", Owner = null })
                    where subpet != null && subpet.Name != String.Empty
                    select new { person.FirstName, PetName = (subpet == null ? String.Empty : subpet.Name) };

        foreach (var v in query)
        {
            Console.WriteLine("{0,-15}{1}", v.FirstName + ":", v.PetName);
        }
    }
}
类程序
{
静态void Main(字符串[]参数)
{
LeftOuterJoinExample();
Console.ReadLine();
}
班主任
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
}
等级宠物
{
公共字符串名称{get;set;}
公共人物所有者{get;set;}
}
公共静态void LeftOuterJoinExample()
{
Person magnus=新人{FirstName=“magnus”,LastName=“Hedlund”};
Person terry=新人{FirstName=“terry”,LastName=“Adams”};
Person charlotte=新人{FirstName=“charlotte”,LastName=“Weiss”};
personarlene=新的人{FirstName=“arlene”,LastName=“Huff”};
宠物大麦=新宠物{Name=“大麦”,所有者=特里};
宠物靴=新宠物{Name=“boots”,所有者=特里};
宠物胡须=新宠物{Name=“胡须”,所有者=夏洛特};
宠物蓝月亮=新宠物{Name=“蓝月亮”,主人=特里};
宠物雏菊=新宠物{Name=“daisy”,主人=马格纳斯};
//创建两个列表。
名单人物=新名单{magnus、terry、charlotte、arlene};
宠物名单=新名单{大麦、靴子、胡须、蓝月亮、雏菊};
var query=来自人中的人
加入宠物对人等于宠物。主人进入gj
来自gj.DefaultIfEmpty(new Pet(){Name=”“,Owner=null}中的子集
其中subpet!=null&&subpet.Name!=String.Empty
选择新的{person.FirstName,PetName=(subpet==null?String.Empty:subpet.Name)};
foreach(查询中的var v)
{
WriteLine(“{0,-15}{1}”,v.FirstName+“:”,v.PetName);
}
}
}

这并不能回答这个问题。若要评论或要求作者澄清,请在他们的帖子下方留下评论。我知道这不是一个非常完整的答案,但它仍然是OP所面临问题的正确答案。这是来自“低质量帖子”评论队列的自动消息。它确实回答了这个问题,但我认为它对OP没有任何帮助,也不够详细。我想,如果我是OP,我会回答你“嗯,但是怎么做?”。@DipalKothari:如果它解决了你的问题,请不要忘记标记答案已被接受。@DotNetHitMan谢谢你的回答。这真的很有帮助