Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6610547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:51:16+00:00 2026-05-25T19:51:16+00:00

I’m having a problem getting some data since I’m just starting to use LINQToEntities,

  • 0

I’m having a problem getting some data since I’m just starting to use LINQToEntities, Entity Framework and Lambda Expressions.

Let me explain my case:

I have a database with 4 tables as shown here:

Database diagram

when I generate the model from database in Visual Studio (2010) the result is this:

Model in Visual Studio

I searched for some info and turns out that since the table t_user_role only has the ids from its two parent tables, it gets abstracted in the model, and you have to use the navigation properties instead.

I had some problem getting the role info for a user on a given system (as the next function does)

    public t_role GetRoleForUser(string userId, string sysId)
    {
        entities = new secfinEntities(); //context from the model

        t_role userRole = entities.t_role.Where(r => r.t_user.Any(u => u.uid == userId) & r.sys_id == sysId).First();

        return userRole;
    }

Now I have to implement a simple search function that will look users that contain the provided string and return the user’s id and name (uid, user_name) and their role’s info (role_id, role_name) on a given system (the system info i have beforehand), so basically I wanna turn this next SQL Query into Lambda Expressions (keeping in mind that in the model the table t_user_role has been abstracted)

SELECT U.uid, U.user_name, R.role_id, R.role_name
FROM t_user U
    INNER JOIN t_user_role UR ON U.uid = UR.uid
    INNER JOIN t_role R ON UR.role_id = R.role_id
WHERE R.sys_id = @p0 -- first parameter
    AND U.user_name LIKE '%' + @p1 + '%' -- second parameter

Also, I would like to store the results of that in a List of a type I have defined as follows:

public class UserRole
{
    public string UserId { get; set; }
    public string UserName { get; set; }
    public string RoleId { get; set; }
    public string RoleName { get; set; }

    public UserRole(string uid, string uname, string rid, string rname)
    {
        UserId = uid;
        UserName = uname;
        RoleId = rid;
        RoleName = rname;
    }
}

So after explaining what I have done and what I’m trying to do first question is: how can that be done?, second: can the same be accomplished through the verbose form instead of Lambda expressions? if yes, how?

Thanks a lot in advance for your time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:51:17+00:00Added an answer on May 25, 2026 at 7:51 pm

    This T-SQL:

    SELECT U.uid, U.user_name, R.role_id, R.role_name 
    FROM t_user U 
        INNER JOIN t_user_role UR ON U.uid = UR.uid 
        INNER JOIN t_role R ON UR.role_id = R.role_id 
    WHERE R.sys_id = @p0 -- first parameter 
        AND U.user_name LIKE '%' + @p1 + '%' -- second parameter 
    

    given your model translates to this verbose syntax (including the requirement to use your new model):

    var results = (from u in entities.t_user
                  from r in u.t_role
                  where r.sys_id == sysIdVariable && u.user_name.Contains(userNameVariable)
                  select new UserRole(u.uid, u.user_name, r.role_id, r.role_name))
    

    I know you didn’t ask for it, but a lambda version might look like:

    var results = entities.t_user.Join(entities.t_role, 
                                       x => x.t_role_id, 
                                       x => x.role_id, 
                                      (u, r) => new UserRole(u.uid, 
                                                             u.user_name, 
                                                             r.role_id, 
                                                             r.role_name))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.