none
Async Linq Expression in Schnittstellendefinition aufnehmen RRS feed

  • 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

    Montag, 27. Mai 2013 12:32

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
    Montag, 27. Mai 2013 13:34
    Moderator

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
    Montag, 27. Mai 2013 13:34
    Moderator
  • 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
    Montag, 27. Mai 2013 13:50
  • Hi Christian,

    ja, einen LINQ-Provider für einen Web-Dienst.
    Aber was fehlt dir denn noch für die Beantwortung deiner aktuellen Frage?

    Gruß
    Marcel

    Montag, 27. Mai 2013 14:03
    Moderator
  • Hi Marcel,

    ich werde noch einen Linq Provider für Ldap implementieren müssen. Da hätte mich nur interessiert, ob Du evtl. hierfür einen Leitfaden kennst. Aber das hängt nicht so wirklich mit der Frage zusammen.

    Viele Grüße,
    Christian

    Montag, 27. Mai 2013 14:06