Help on using filter on LLBLGEN

Posts   
 
    
Prem Singh
User
Posts: 7
Joined: 21-Jun-2011
# Posted on: 21-Jun-2011 12:18:56   

i am having two tables member and message.

In message have memberGuid and friendGuid as two columns.

I will be a logged in user and my Guid will be in either memberGuid or friendGuid.

I want to get the list of friendGuids (if my guid is on memberGuid) and memberGuid (if my Guid is on friendGuid). on basis of Entity collection of members.

Please help me to find a solution for the above case. I am new to llblgen. Thanks in advance...

Regards, Prem Singh

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 21-Jun-2011 12:56:09   

Something like:

SELECT friendGuid FROM Message WHERE memberGuid = myID
SELECT memberGuid FROM Message WHERE friendGuid = myID

If this is exactly what you want, then you should for each query use a DynamicList to fetch one field, and the rest should be pretty easy.

Prem Singh
User
Posts: 7
Joined: 21-Jun-2011
# Posted on: 21-Jun-2011 13:15:16   

Actually i am having the code as follows



EntityCollection<MemberEntity> members = new EntityCollection<MemberEntity>();
RelationPredicateBucket bucket = new RelationPredicateBucket();
bucket.PredicateExpression.Add(MemberFields.MemberStatus == 1);
bucket.Relations.Add(MemberEntity.Relations.BlinkMatchCalculateScoreEntityUsingMemberGuid);
bucket.Relations.Add(MemberEntity.Relations.BlinkMatchCalculateScoreEntityUsingFriendGuid);
bucket.PredicateExpression.AddWithAnd(BlinkMatchCalculateScoreFields.Percentage >= 75);
// need the next filter here

Now i want to write another filter that in BlinkMatchCalculateScoreEntity i am having memberGuid and friendGuid. I want to get friendGuid in member collection if myid is in memberGuid or get memberGuid in membercollection if myid is in friendguid.

Please help.....

Walaa wrote:

Something like:

SELECT friendGuid FROM Message WHERE memberGuid = myID
SELECT memberGuid FROM Message WHERE friendGuid = myID

If this is exactly what you want, then you should for each query use a DynamicList to fetch one field, and the rest should be pretty easy.

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 21-Jun-2011 20:50:47   

You can add to the PredicateExpression to allow you to filter on fields that you have added via the relations.


bucket.PredicateExpression.Add(BlinkMatchCalculateScoreEntityFields.FriendGuid == myId);

Matt

Prem Singh
User
Posts: 7
Joined: 21-Jun-2011
# Posted on: 24-Jun-2011 06:02:44   

This will give me the query to search for just the existence of myId in FriendGuid, Actually i want to get the FriendGuid if myId is in MemberGuid Column and MemberGuid if myId is in FriendGuid.

MTrinder wrote:

You can add to the PredicateExpression to allow you to filter on fields that you have added via the relations.


bucket.PredicateExpression.Add(BlinkMatchCalculateScoreEntityFields.FriendGuid == myId);

Matt

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 24-Jun-2011 11:03:50   

Wht you want to do is not 100% clear to me. So if you could come up with the SQL Query you need to implement, we would be able to help you implement it with LLBLGen code.

Prem Singh
User
Posts: 7
Joined: 21-Jun-2011
# Posted on: 24-Jun-2011 16:15:25   

I am having a table named “Member” with Primary Key as MemberGuid

I am having another Table named “FriendRequest” with fields “RequestedBy” and “RequestedFrom” both are Foreign Key of MemberGuid of “Member” table.

Suppose the table structure of FriendRequest is as follows

RecordID RequestedBy RequestedFrom 1 1 2 2 3 5 3 2 5 4 5 1 5 2 3

Now I am a currently logged in User with MemberGuid Say “2”.

Now I require the List of members from which I get the requests

Ie Here in the table In recorded 1 my member 2 is in “RequestedFrom” so I need 1 and in record 3 I am in “RequestedBy” so require 5 and so on.

So my final out put should be

MemberCollection of 1, 5, 3

Please help me obtain this logic by adding the pending Code to obtain the above said output….

EntityCollection<MemberEntity> members = new EntityCollection<MemberEntity>();
RelationPredicateBucket bucket = new RelationPredicateBucket();
bucket.PredicateExpression.Add(MemberFields.MemberStatus == 1);
bucket.Relations.Add(MemberEntity.Relations.BlinkMatchCalculateScoreEntityUsingMemberGuid);
bucket.Relations.Add(MemberEntity.Relations.BlinkMatchCalculateScoreEntityUsingFriendGuid);
bucket.PredicateExpression.AddWithAnd(BlinkMatchCalculateScoreFields.Percentage >= 75);
// need the next filter here

Please help me I am really unaware of how to proceed.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 25-Jun-2011 06:27:32   

First you have to analyze how the SQL would look like, then see what LLBLGen objects you can use to get this to work, finally write your code.

I can just write you an approximate code. Instead I will show you a similar scenario. Consider the following:

EmployeeId - ReportsTo 1 2 2 NULL 3 2 4 2 5 2 6 5 7 5 8 2 9 5

Now say you want get all the employees related to "Employee 5", so if he is the boss you get the subordinate, if he is a subordinate you get the boss. In this case you expect to get employees "2,6,7,9".

To get this in SQL you would write something like:

SELECT *
FROM Employees
WHERE EmployeeId IN
    (SELECT CASE (EmployeeId) WHEN @id THEN ReportsTo ELSE EmployeeId END
     FROM Employees
     WHERE EmployeeId = @id OR ReportsTo = @id)

We are close. We just need to translate this to LLBLGen. the "WHERE EmployeeId IN" says us that we need to use a FieldCompareSetPredicat, and the "CASE ..." statement could be written used a DBFunctionCall.

So now it's time to write code. Here is the code for the above example:

[TestMethod]
public void GetAllRelatedEmployeesToAnEmployee()
{
    int employeeIdToSearch = 5;

    // expression to select the right field to pick up
    IExpression employeeFieldToUseExpr = new DbFunctionCall("CASE {0} WHEN " + employeeIdToSearch.ToString() + " THEN {1} ELSE {2} END",
        new object[] { EmployeeFields.EmployeeId, EmployeeFields.ReportsTo, EmployeeFields.EmployeeId });

    // WHERE EmployeeId IN ... filter
    IPredicateExpression relatedEmployeeSubFilter = new PredicateExpression (EmployeeFields.EmployeeId == employeeIdToSearch  | EmployeeFields.ReportsTo == employeeIdToSearch);

    FieldCompareSetPredicate relatedEmployeeFilter = new FieldCompareSetPredicate(EmployeeFields.EmployeeId,
        null, EmployeeFields.EmployeeId.SetExpression(employeeFieldToUseExpr), null, SetOperator.In,
        relatedEmployeeSubFilter);

    // add it to the main filter
    IRelationPredicateBucket filter = new RelationPredicateBucket();
    filter.PredicateExpression.Add(relatedEmployeeFilter);

    // fetch the collection
    EntityCollection<EmployeeEntity> employees = new EntityCollection<EmployeeEntity>();
    using (var adapter = new DataAccessAdapter())
    {
        adapter.FetchEntityCollection(employees, filter);
    }


    // test
    var ids = string.Join(",",
        (from e in employees
            select e.EmployeeId.ToString()));

    Assert.AreEqual("2,6,7,9", ids);
}

So, now you can write your code. This is an approximate code (not tested):

// expression to select the right field to pick up
IExpression blinkMatchFieldToUseExpr = new DbFunctionCall("CASE {0} WHEN " + myId.ToString() + " THEN {1} ELSE {2} END",
        new object[] {BlinkMatchCalculateScoreFields.MemeberGuid, BlinkMatchCalculateScoreFields.FiendGuid, BlinkMatchCalculateScoreFields.MemeberGuid });

// WHERE Member.MemberGuid IN ... filter
IPredicateExpression relatedBlinkMatchSubFilter = new PredicateExpression (
         BlinkMatchCalculateScoreFields.MemeberGuid == myId  | BlinkMatchCalculateScoreFields.FriendGuid == myId);

FieldCompareSetPredicate relatedBlinkMatchFilter = new FieldCompareSetPredicate(MemberFields.MemeberGuid,
        null, BlinkMatchCalculateScoreFields.MemeberGuid.SetExpression(blinkMatchFieldToUseExpr), null, SetOperator.In,
        relatedBlinkMatchSubFilter );


// here is your code
EntityCollection<MemberEntity> members = new EntityCollection<MemberEntity>();
RelationPredicateBucket bucket = new RelationPredicateBucket();
bucket.PredicateExpression.Add(MemberFields.MemberStatus == 1);
bucket.Relations.Add(MemberEntity.Relations.BlinkMatchCalculateScoreEntityUsingMemberGuid);
bucket.Relations.Add(MemberEntity.Relations.BlinkMatchCalculateScoreEntityUsingFriendGuid);
bucket.PredicateExpression.AddWithAnd(BlinkMatchCalculateScoreFields.Percentage >= 75);


// need the next filter here
bucket.PredicateExpression.Add(relatedBlinkMatchFilter);
David Elizondo | LLBLGen Support Team
Prem Singh
User
Posts: 7
Joined: 21-Jun-2011
# Posted on: 29-Jun-2011 06:41:29   

Thanks a lot, it help me a lot