开发者

C# LINQ question

I have a series of type List<Int32> and another series of List<ClassA>, where ClassA has the following representation :

public ClassA
{
  public String PropertyA {get; set;}
  public List<Int32> PropertyB {get; set;}
  public String P开发者_运维技巧ropertyC {get; set;}
}

I want to retrieve using LINQ a series of type List<KeyValuePair<PropertyC, Int32>> where Int32 is contained within List<Int32> PropertyB. Can this be done using one linq statement?


I think you're looking for something like:

var query = (from x in listOfInt32s
             from y in listOfClassAs
             where y.PropertyB.Contains(x)
             select new KeyValuePair<string, int>(y.PropertyC, x))
            .ToList();

... but I'm not entirely sure.

(As per the comments, I've changed the first KeyValuePair type argument to string.)


var q = (from i in listOfInts
         from a in listOfA
         where
              a.PropertyB.Contains(i)
         select new KeyValuePair<String, int>(a.PropertyC, i))
         .ToList();


Not completely sure what you mean by "where Int32 is contained within List<Int32> PropertyB". I'm interpreting it to mean that you want the value part of the KeyValuePair to be some integer that's in PropertyB. In the example below I pick the first one.

List<ClassA> myList = GetListOfStuff(); //However you get the list
var results = from myList
              select new KeyValuePair<string, int>(a.PropertyC, a.PropertyB.First());


var idList = GetIdList(); //List of integer
var objectList = GetObjectList(); //List of ClassA
var pairs = objectList
    .SelectMany(item => item.PropertyB
        .Select(id => new KeyValuePair<string, int>(item.PropertyC, id)))
    .Where(item => idList.Contains(item.Value));
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜