none
Help Needed in Entity Call RRS feed

  • Question

  • Hi,

    i am working c#4.5 and Below is my sample code. I am passing the Name to the entity call and i need to get the firstname which matches the input parameter. If first name doesn't exists then get the lastname.

    It's quite easy doing this using coalesce in sqlserver. Is there anyway we can do this in  entity call?
    any sample code please

    private void GetUserNames(string Name)
    {
    UserEntities userEntityDb = new UserEntities();
                List<users> objList = CompiledQuery.Compile((UserEntities UserEntity) =>
                               from objList in UserEntity.users
    			where FirstName = Name
                               select objList).Invoke(userEntityDb).ToList();
                return objList;
    }


    loving dotnet


    • Edited by Born2Achieve Friday, October 10, 2014 11:41 AM
    • Moved by CoolDadTx Friday, October 10, 2014 1:57 PM EF related
    Friday, October 10, 2014 11:40 AM

Answers

  • Hello Born2Achieve,

    Try linq query below:

    var result = (from t in db.Tables
    
                                  where (t.FirstName == null ? t.LastName : t.FirstName) == "2"
    
                                  select t).ToList();
    

    It would find two records if the data is like below:

    ID            FirstName           LatsName
    
    1              2              1         
    
    2              NULL     2
    

    And please note that as darnold924 mentioned, in LINQ, we need to use “==”.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Born2Achieve Monday, October 13, 2014 11:14 AM
    Monday, October 13, 2014 5:36 AM
    Moderator

All replies

  •  where FirstName == Name
    Sunday, October 12, 2014 1:51 AM
  • Hello Born2Achieve,

    Try linq query below:

    var result = (from t in db.Tables
    
                                  where (t.FirstName == null ? t.LastName : t.FirstName) == "2"
    
                                  select t).ToList();
    

    It would find two records if the data is like below:

    ID            FirstName           LatsName
    
    1              2              1         
    
    2              NULL     2
    

    And please note that as darnold924 mentioned, in LINQ, we need to use “==”.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Born2Achieve Monday, October 13, 2014 11:14 AM
    Monday, October 13, 2014 5:36 AM
    Moderator
  • Hi Fred,

    Wonderful. Thanks a lot for the help


    loving dotnet

    Monday, October 13, 2014 11:14 AM