Click here to Skip to main content
15,905,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm stacking with Join LINQ in C#

I have this Query in SQL server :

SELECT cu.UserName, cu.DruckerA4,fd.Beschreibung
FROM Config_User cu
JOIN Filiale_User fu ON fu.UserID = cu.UserID
JOIN Filialdaten fd ON fd.FilialID = fu.FilialID


and i'm trying to write a LINQ code with this Query. it should do the same work .
what i have done until now is getting the elments from ConfigUser in a Method but then i am stacking how to do Join with anotehr tables .

i want to join Filialdaten and Filiale_User with Config_User

What I have tried:

public List<ConfigUser> GetUser(List<ConfigUser> elements)
       {
           using (IDbConnection connection = new SqlConnection (CS))
           {
               connection.Open();
               elements = connection.Query<ConfigUser>(" select UserName,DruckerA4 from Config_User").ToList();

           }
           return elements;
       }


the Join Method which i am stacking with :
public void JoinTable(List<ConfigUser> elements)
       {
           using (IDbConnection connection = new SqlConnection(CS))
           {
               connection.Open();
               var innerJoin = from cu in elements
                               join
             }
       }
Posted
Updated 9-Sep-19 2:23am

1 solution

I'd strongly recommend to read this: Perform inner joins (LINQ in C#) | Microsoft Docs[^]

Now, check this:
C#
var qry = (from cu in Config_User
    join fu in Filiale_User on fu.UserID equals cu.UserID
    join fd in Filialdaten on fd.FilialID equals fu.FilialID
    select new LoadResult
    (
        UserName = cu.UserName,
        DruckerA4 = cu.DruckerA4,
        Beschreibung = fd.Beschreibung
    )).ToList();
 
Share this answer
 
v2
Comments
[no name] 9-Sep-19 11:08am    
so i did the following :
public IEnumerable<loadresult> JoinTable(List<configuser> ConfigsUser, List<filiale_user> FilialU, List<filialdaten> FilialData)
{
var query = from cu in ConfigsUser
join fu in FilialU on cu.USerId equals fu.UserID
join fd in FilialData on fu.FilialId equals fd.FilialID
select new LoadResult
{
UserName = cu.UserName,
DruckerA4 = cu.DruckerA4,
Beschreibung = fd.Beschreibung
};

var list = query.ToList();
return query;
}
but i still face a problmen which is :
the var list is 0
if i debug on return query i will find Non-Public member then 'inner' which has Count for 29 and that are the value i need . and on results view i find 'Enumeration yielded no results' and the end i can't get the values .
Maciej Los 10-Sep-19 2:30am    
Well... i have no access to your data and can't read direct from your screen. So, you have to debug the programm to find out where the reason of such strange behaviour is.
[no name] 10-Sep-19 3:45am    
I am still doing .. will see . thanks for your help
Maciej Los 10-Sep-19 4:37am    
You're very welcome.
Good luck!
[no name] 10-Sep-19 5:26am    
I found the Bug and its working now as it should be . i am using Dapper and one of the prop is not selected from select statment thus has 0000 value and can't find the FK in the other Table that's why was this strange behaviour .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900