Complex Linq query is not working as expected

后端 未结 2 843
粉色の甜心
粉色の甜心 2021-01-07 14:05

I want to combine results from 4 tables and select specific fields using LINQ. Please bear with me since I have not done complex LINQ queries.

Table 1 - Subscriber<

2条回答
  •  一向
    一向 (楼主)
    2021-01-07 14:49

    It's not really LINQ that's tripping up here, it's LINQ to Entities. Are you using Entity Framework? Does your model have the relationships defined in it? If you have foreign keys in your database, and build your model in Entity Framework with database first, it will map all the entity relationships for you.

    If yes, you can then do something like:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public class Program
    {
        public class Subscriber{
            public string Name {get;set;}
            public List Subscriptions{get;set;}
        }
    
        public class Subscription{
            public string Name {get;set;}
        }
    
        public class MyViewModelItem{
            public string SubscriberName{get;set;}
            public string SubscriptionNames {get;set;}
        }
    
        public static void Main()
        {
            Console.WriteLine("Hello World");
    
            // create some dummy data
            var data = new List{
                new Subscriber{
                    Name = "Arnold",
                    Subscriptions = new List(){
                        new Subscription{
                            Name = "Subscription A"
                        },
                        new Subscription{
                            Name = "Subscription B"
                        },
                        new Subscription{
                            Name = "Subscription C"
                        }
                    }
                },
                new Subscriber{
                    Name = "Betty",
                        Subscriptions = new List()
                },
                new Subscriber{
                    Name = "Christopher",
                    Subscriptions = new List(){
                        new Subscription{
                            Name = "Subscription A"
                        }
                    }
                }
            };
    
            // here's the query and it becomes much simpler
            var myViewModel = data
                .Select(i=> new MyViewModelItem{
                    SubscriberName = i.Name,
                    SubscriptionNames = string.Join(", ", i.Subscriptions.Select(j=>j.Name))
                })
                .ToList();
    
            // this shows the output
            foreach(var item in myViewModel){
                Console.WriteLine(string.Format("subscriber: {0}, subscriptions: {1}",item.SubscriberName,item.SubscriptionNames));
            }
    
        }
    }
    

    Output:

    Hello World subscriber: Arnold, subscriptions: Subscription A, Subscription B, Subscription C subscriber: Betty, subscriptions: subscriber: Christopher, subscriptions: Subscription A

提交回复
热议问题