Benutzer mit den meisten Antworten
Async Linq Expression in Schnittstellendefinition aufnehmen

Frage
-
Hi,
ich möchte eine asynchrone Methode in meiner Schnittstelle definieren, die eine Linq Expression übernimmt. Diese soll einen entsprechenden Task<???> liefern. Es wird nach Kontakten gesucht.
Wie würde bitte eine solche Methodensignatur aussehen?
Viele Grüße,
Christian
Antworten
-
Hallo Christian,
Wenn ich dich richtig verstehe, würde ich folgendes schreiben:
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { new Program().Test(); Console.ReadKey(true); } public async void Test() { ContactsManager manager = new ContactsManager(); IQueryable<Contact> t =
await manager.GetContacts(c => c.LastName == "Bäcker" && c.Email.Contains("example.com")); var result = t.ToList(); } } public class ContactsManager { List<Contact> contacts = null; public ContactsManager() { this.contacts = new List<Contact> { new Contact { FirstName = "Tom", LastName = "Bäcker", Email = "tom@albert.de", PhoneNo="09999" }, new Contact { FirstName = "Jenny", LastName = "Bäcker", Email = "jenny@example.com", PhoneNo="09999" }, new Contact { FirstName = "Hannes", LastName = "Domeyer", Email = "hannes@domeyer.de", PhoneNo="09999" } }; } public Task<IQueryable<Contact>> GetContacts(Expression<Func<Contact, bool>> selectionCriteria) { TaskCompletionSource<IQueryable<Contact>> tcs = new TaskCompletionSource<IQueryable<Contact>>(); IQueryable<Contact> query = contacts.Where(selectionCriteria.Compile()).AsQueryable(); tcs.SetResult(query); return tcs.Task; } } public class Contact { public string FirstName { get; set; } public string LastName { get; set; } public string PhoneNo { get; set;} public string Email { get; set;} } }
Gruß
Marcel
- Als Antwort markiert Christian315 Montag, 27. Mai 2013 13:50
- Tag als Antwort aufgehoben Christian315 Montag, 27. Mai 2013 13:55
- Als Antwort markiert Christian315 Montag, 27. Mai 2013 14:06
Alle Antworten
-
Hallo Christian,
Wenn ich dich richtig verstehe, würde ich folgendes schreiben:
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { new Program().Test(); Console.ReadKey(true); } public async void Test() { ContactsManager manager = new ContactsManager(); IQueryable<Contact> t =
await manager.GetContacts(c => c.LastName == "Bäcker" && c.Email.Contains("example.com")); var result = t.ToList(); } } public class ContactsManager { List<Contact> contacts = null; public ContactsManager() { this.contacts = new List<Contact> { new Contact { FirstName = "Tom", LastName = "Bäcker", Email = "tom@albert.de", PhoneNo="09999" }, new Contact { FirstName = "Jenny", LastName = "Bäcker", Email = "jenny@example.com", PhoneNo="09999" }, new Contact { FirstName = "Hannes", LastName = "Domeyer", Email = "hannes@domeyer.de", PhoneNo="09999" } }; } public Task<IQueryable<Contact>> GetContacts(Expression<Func<Contact, bool>> selectionCriteria) { TaskCompletionSource<IQueryable<Contact>> tcs = new TaskCompletionSource<IQueryable<Contact>>(); IQueryable<Contact> query = contacts.Where(selectionCriteria.Compile()).AsQueryable(); tcs.SetResult(query); return tcs.Task; } } public class Contact { public string FirstName { get; set; } public string LastName { get; set; } public string PhoneNo { get; set;} public string Email { get; set;} } }
Gruß
Marcel
- Als Antwort markiert Christian315 Montag, 27. Mai 2013 13:50
- Tag als Antwort aufgehoben Christian315 Montag, 27. Mai 2013 13:55
- Als Antwort markiert Christian315 Montag, 27. Mai 2013 14:06
-
Hi Marcel,
very cool :-)
Hast Du evtl. auch schon mal einen Linq-Provider geschrieben? Hast Du eine Idee, wie aufwändig das wäre? Ich bräuchte einen, der mir aus der Expression einen Ldap Request erstellt.
Vielen Dank und viele Grüße,
Christian
- Bearbeitet Christian315 Montag, 27. Mai 2013 13:52 Unvollständig