如何将LINQ to SQL分组结果映射到此现有接口?

时间:2010-01-05 14:54:15

标签: c# linq linq-to-sql

我有一个LINQ to SQL查询,它返回Sponsor个对象的分组集合,如下所示:

var result = ( from s in db.Sponsors
               join sl in sb.SponsorLevels on s.SponsorLevelId equals sl.SponsorLevelId
               select new Sponsor 
               {
                 Name = s.Name,
                 Level = sl.LevelName
               }
             ).GroupBy(s => s.LevelName);

我的应用程序已使用定义为...的接口

public interface ISponsorLevelGroup
{
    string Level { get; set; }
    IList<Sponsor> Sponsors { get; set; }
}

...其中Level字符串属性是LINQ结果中的分组键。我最终希望将LINQ结果变为IList<ISponsorLevelGroup>

逻辑上我的LINQ to SQL结果是ISponsorLevelGroup个对象的集合,但我不确定如何最有效地将一个对象映射到另一个。我似乎被降级为此时手动遍历组和其中的集合,并将数据复制到实现我的特定接口的对象中。有没有更好的办法?如何才能最有效地将我的LINQ结果推送到IList<ISponsorLevelGroup>

2 个答案:

答案 0 :(得分:1)

你试过了吗?

private class SponsorLevelGroup : ISponsorLevelGroup
{
   public string Level { get; set; }
   public IList<Sponsor> Sponsors { get; set; }
}

var result = ( from s in db.Sponsors
               join sl in sb.SponsorLevels on s.SponsorLevelId equals sl.SponsorLevelId
               select new Sponsor 
               {
                 Name = s.Name,
                 Level = sl.LevelName
               }
             ).GroupBy(s => s.LevelName)
              .Select(g => new SponsorLevelGroup
                               {
                                   Level = g.Key,
                                   Sponsors = g.ToList()
                                }) ;

这应该返回一个集合ISponsorLevelGroup对象

答案 1 :(得分:0)

这是你需要的吗?我没有像你那样创建一个新的赞助商类。看看这是否有意义。

    public class Sponsor
    {
        public int SponsorLevelId { get; set; }
        public string Name { get; set; }

        public override string ToString()
        {
            return string.Format("Name: {0}", Name);
        }
    }

    public class SponsorLevel
    {
        public int SponsorLevelId { get; set; }
        public string LevelName { get; set; }
    }

    public class SponsorLevelGroup
    {
        public string Level { get; set; }
        public IList<Sponsor> Sponsors { get; set; }

        public override string ToString()
        {
            return string.Format("Level: {0} Sponsors: {1}", Level, Sponsors.Count);
        }
    }

    static void Main(string[] args)
    {
        List<Sponsor> sponsors = new List<Sponsor>()
        {
            new Sponsor { SponsorLevelId = 1, Name = "A" },
            new Sponsor { SponsorLevelId = 2, Name = "B" },
            new Sponsor { SponsorLevelId = 1, Name = "C" },
            new Sponsor { SponsorLevelId = 3, Name = "D" }
        };

        List<SponsorLevel> sponsorLevels = new List<SponsorLevel>()
        {
            new SponsorLevel { SponsorLevelId = 1, LevelName = "L1" },
            new SponsorLevel { SponsorLevelId = 2, LevelName = "L2" },
            new SponsorLevel { SponsorLevelId = 3, LevelName = "L3" }
        };

        var result = (from s in sponsors
                      join sl in sponsorLevels on s.SponsorLevelId equals sl.SponsorLevelId
                      group s by sl.LevelName into g
                      select new SponsorLevelGroup
                      {
                          Level = g.Key,
                          Sponsors = g.ToList()
                      }
         );

        foreach (var r in result)
        {
            Console.WriteLine(r);
        }
     }

打印:

Level: L1 Sponsors: 2
Level: L2 Sponsors: 1
Level: L3 Sponsors: 1