简体   繁体   中英

Relate more than one column with lambda expression

I have the query below that relates only the ProcessId. I need to know how to relate the lists processes and pendingProcess by ProcessId and GroupId. Thankful.

        return process.Join(
            pendingProcess,
            p => p.ProcessId,
            pp => pp.ProcessId,
            (p, pp) => new Process
            {
                ProcessId = p.ProcessId,
                GroupId = p.GroupId,
                Text = p.Text,
            }

        ).ToList();

Use composite join condition via anonymous types:

return process.Join(pendingProcess,
    p => new { p.ProcessId, p.GroupId }
    pp => new { ProcessId = (int)pp.ProcessId, pp.GroupId }
    (p, pp) => new Process
    {
        ProcessId = p.ProcessId,
        GroupId = p.GroupId,
        Text = p.Text,
    }).ToList();

If you prefer lambda expression;

return process
    .Where(p => pendingProcess.Any(pp=> pp.GroupId == p.GroupId && pp.Precessid == p.PreocessId))
    .Select(p=> new Process
            {
                ProcessId = p.ProcessId,
                GroupId = p.GroupId,
                Text = p.Text,
            }).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM