Question

I am trying to set up a LINQ query to use as a combobox datasource. The combobox is a simple list of names for DisplayMember and PeopleID for ValueMember.

The catch is that the name could be in First Last or Last, First format. I have a GetFullName(intPeopleID) function to return the correct name Sequence. The combobox names must be sorted in alpha order either way, so the sorting needs to be done after the first query gets the full names.

Dim LQBackupOriginalA = From a In DCAppMain.tblPeopleMain
        Where a.BusinessGroupID = intBusinessGroupID
        Select Person = PP.GetFullName(a.PeopleID), a.PeopleID

Dim LQBackupOriginalB = From b In LQBackupOriginalA
    Order By b.Person
    Select b.Person, b.PeopleID

cboBackupOriginal.DataSource = LQBackupOriginalA
cboBackupOriginal.DisplayMember = "Person"
cboBackupOriginal.ValueMember = "PeopleID"

This doesn't work because the function 'GetFullName' does not translate to SQL - of course. I've tried numerous variations on this but no success.

I could first append the data from the first query into a local table, and then pull it back out with a query that sorts the data prior to setting the combobox's datasource. But I wonder if there is something more straightforward?

Was it helpful?

Solution 2

Thanks Joachim - I haven't tried using lambda functions till now and they are helpful!

This is what I finally did that does work:

    '-- Get Distinct ManagerID
    Dim LQManagerOriginalA = (From a In DCAppMain.tblPeopleMain
        Where a.ManagerID IsNot Nothing And a.CurrentEmployee = True And a.UserType = "Full"
        Group By a.ManagerID Into g = Group
        Order By ManagerID
        Select New With {.ManagerID = ManagerID})

    '-- Get Ordered List of FullNames and PeopleIDs
    Dim LQManagerOriginalB = LQManagerOriginalA.[Select](Function(a) a.ManagerID).AsEnumerable().[Select](Function(a) New With {Key .Person = PP.GetFullName(a), Key .PeopleID = a}).OrderBy(Function(x) x.Person).ToList()

    '-- Apply datasource to combobox        
    RemoveHandler cboManagerOriginal.SelectedIndexChanged, AddressOf cboManagerOriginal_SelectedIndexChanged
    cboManagerOriginal.DataSource = LQManagerOriginalB
    AddHandler cboManagerOriginal.SelectedIndexChanged, AddressOf cboManagerOriginal_SelectedIndexChanged
    cboManagerOriginal.DisplayMember = "Person"
    cboManagerOriginal.ValueMember = "PeopleID"

OTHER TIPS

You should be able to just filter the rows you want, convert the query to an enumerable and do the GetFullName part in memory instead. My VB is lacking, but something similar to this;

Dim LQBackupOriginalA = (
    From a In DCAppMain.tblPeopleMain
    Where a.BusinessGroupID = intBusinessGroupID
    Select a.PeopleID).
         AsEnumerable().
         Select(Function(peopleId) New With {
            Key .Person = PP.GetFullName(peopleId), .PeopleID = peopleId
         })

...or in C# where I'm sure of the syntax;

var LQBackupOriginalA = 
    DCAppMain.tblPeopleMain
        .Where(a => a.BusinessGroupID == intBusinessGroupID)
        .Select(a => a.PeopleID)
        .AsEnumerable()
        .Select(peopleId => 
                  new {Person = PP.GetFullName(peopleId), PeopleID = peopleId});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top