开发者

How to use projection to select where x.val1=1, x.val2=2, etc.?

How can use projection to select with where clause specification?

Perhap开发者_如何学编程s something like?:

List<string> Titles = 
  iEnumerableResultSet.Select(x => x.Title = "Whatever", x=> x.Id =5).ToList();


In my opinion, Select is poorly named. It is suppose to remind you of SELECT from SQL. That is, you specify the values that you want for each object in collection that you are Selecting over (if you know functional programming Select is the same as map).

To filter, what you want is Where:

var filteredResultSet = 
    iEnumerableResultSet
       .Where(x => x.Title == "Whatever" && x.Id == 5)
       .ToList();

How can use projection to select with where clause specification?

Now, your question seems to be asking how to both filter and project. You can say

var titles =
    iEnumerableResultSet
        .Where(x => x.Id == 5) // filter
        .Select(x => x.Title)  // project
        .ToList();  

You can think of this as being like the SQL query

SELECT Title
FROM SomeTable
WHERE Id = 5  


'Select' isn't the extension method you want for filtering, you want to use 'Where' instead. In this sense, the 'Select' is for selecting parts of the input objects to pass along ('projection' creation), not for deciding which objects do and don't get filtered out.

So, you want something like:

var title = enumerableResultSet.Where(x => x.Title == "Whatever" || x.Id == 5).ToList();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜