简体   繁体   中英

How can I filter WCF service results using lambda / linq

I am planning a WCF service to return lists, data, all the usual things.

Now I saw on here ages ago that as of .NET 4.5 you could pass lambda expressions or filters to WCF (I also saw something in a pluralsight video somewhere) that allowed you to write something along the lines of

IQuerable<string> GetInfo();
// or
List<string> GetInfo(Expression predicate);

Instead of

GetInfo(int page, int resultsPerPage, bool sortAsc, string sortColumn);

However, as I'm reading around I see lots of conflicting (and old) information saying that this isnt possible. Is it then at all possible to filter WCF results before they are returned via some linq or lambda expression?

Update

I have implemented a Service (WCF not WCF Data Services) like so, and I get the expected result. Is the client actually passing the query to the web service or is it being rendered client side?

public class Service1 : IService1
{
    public IQueryable<string> DoWork()
    {
        List<string> strings = new List<string>();
        for (char c = 'a'; c < 'z'; c++)
        {
            strings.Add(c.ToString());
        }

        return strings.AsQueryable();
    }
}

Client:

Service1 s = new Service1();
var results = s.DoWork();
var results1 = results.Where(str => str == "a"); // works

看起来您需要WCF数据服务

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