C
C#2y ago
kommissar

❔ How do I make part of a Linq query optional?

I have a query like this:
var newInfo = from ops in _radanContext.RouteCardOps
join rc in _radanContext.RouteCards on ops.RouteNo equals rc.RouteNo
join con in _radanContext.Contracts on rc.ContractNo equals con.ContractNo
join parts in _radanContext.Parts on new { rc.PartNum, rc.Issue } equals new { parts.PartNum, parts.Issue }
select new CompletedJobResult
{
RouteNo = rc.RouteNo,
Seq = ops.Seq,
OpCode = ops.OpCode,
Issue = rc.Issue,
Description = ops.RouteDesc,
BatchQty = rc.BatchQty,
Qtycomplete = ops.QtyComplete,
ContractNo = con.ContractNo,
CustCode = con.CustCode,
PartNum = parts.Description,
BatchNo = rc.BatchNo
};
var newInfo = from ops in _radanContext.RouteCardOps
join rc in _radanContext.RouteCards on ops.RouteNo equals rc.RouteNo
join con in _radanContext.Contracts on rc.ContractNo equals con.ContractNo
join parts in _radanContext.Parts on new { rc.PartNum, rc.Issue } equals new { parts.PartNum, parts.Issue }
select new CompletedJobResult
{
RouteNo = rc.RouteNo,
Seq = ops.Seq,
OpCode = ops.OpCode,
Issue = rc.Issue,
Description = ops.RouteDesc,
BatchQty = rc.BatchQty,
Qtycomplete = ops.QtyComplete,
ContractNo = con.ContractNo,
CustCode = con.CustCode,
PartNum = parts.Description,
BatchNo = rc.BatchNo
};
My problem is that the contracts should be optional - as sometimes rc.ContractNo can be null. How can I achieve this?
2 Replies
cptcrawler
cptcrawler2y ago
you may think doing a left join so join con in _radanContext.Contracts on rc.ContractNo.DefaultIfEmpty() equals con.ContractNo and if you don't need em just do where != null kinda something like this if this is what you are trying to achieve
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server