开发者

Linq comparing two list - learning linq

开发者 https://www.devze.com 2023-01-15 22:16 出处:网络
I recently started learning LINQ. Bsically to understand this technology better I开发者_JS百科 try to rewrite some of my previous programs using LINQ. I mean I try to replace foreach methods etc with

I recently started learning LINQ. Bsically to understand this technology better I开发者_JS百科 try to rewrite some of my previous programs using LINQ. I mean I try to replace foreach methods etc with linq queries.

Today I encounterd a problem. I have a list of objects element

  List<Element> elementList 
 public  class Element
    {
        private string Id;
        private List<Element> consequentElementsList;


    }

List of elements contains all elements which are placed on form. Each element has a List of consequtive elements. I need to find all predecessors element of element I've chosen. Is there any way to do this in LINQ?


Try this out:

var element = // a single element
var query = from e in elementList
            where e.consequentElementsList.Any(ce => ce.Id == element.Id)
            select e;

It retrieves each element from the element list where the consequent element list contains any element matching the ID you've selected.

Of course, I've ignored the fact that Id and consequentElementsList is private in your example.

0

精彩评论

暂无评论...
验证码 换一张
取 消