将LINQ应用于我的代码上的实体



我有可行的代码,但是我在linq中与实体一起工作,因为我无法弄清楚。您能告诉我如何成功将其应用于我的代码吗?

我所需的结果是词典:

Dictionary<string, SelectedCorffData> dataSelectedForDeletion = new Dictionary<string, SelectedCorffData>();

上述类:

public class SelectedCorffData
{
    public long CorffId { get; set; }
    public string ReportNumber { get; set; }
    public DateTime CorffSubmittedDateTime { get; set; }
}

请注意,我正在循环的" InterSectResult"只是一个字符串集合。

这是我的代码:

DateTime dateToCompare = DateTime.Now.Date;
Dictionary<string, SelectedCorffData> dataSelectedForDeletion = new Dictionary<string, SelectedCorffData>();
foreach (var mafId in intersectResult)
{
    var corffIdsPerMaf = context
        .Mafs
        .Where(m => m.MafId == mafId)
        .Select(m => m.CorffId);
    var corffIdForMaf = context
        .Corffs
        .Where(c => corffIdsPerMaf.Contains(c.Id))
        .OrderByDescending(c => c.CorffSubmittedDateTime)
        .Select(c => c.Id)
        .First();
    //Selected close-out forms, whose MAF's may be up for deletion, based on date.
    var corffData = context
        .Corffs
        .Where(c => c.Id == corffIdForMaf && System.Data.Entity.DbFunctions.AddYears(c.CorffSubmittedDateTime, 1).Value > dateToCompare)                            
        .Select(c => new SelectedCorffData () { CorffId = c.Id,  ReportNumber = c.ReportNumber, CorffSubmittedDateTime = c.CorffSubmittedDateTime })
        .FirstOrDefault();
    if(corffData != null)
    {
        dataSelectedForDeletion.Add(mafId, corffData);
    }
}

请注意:这不仅仅是一个简单的加入。如果无法简化,请告诉我。另请解释原因。

下面的代码我认为完全正确,但它接近您所需的内容。我模拟了数据库,以便可以正确地进行语法。

namespace System
{
    namespace Data
    {
        namespace Entity
        {
            public class DbFunctions
            {
                public static Data AddYears(DateTime submittedTime, int i)
                {
                    return new Data();
                }
                public class Data
                {
                    public int Value { get; set; }
                }
            }
        }
    }
}
namespace ConsoleApplication23
{
    class Program
    {
        static void Main(string[] args)
        {
            Context context = new Context();
            int dateToCompare = DateTime.Now.Year;
            var corffIdsPerMaf = context.Mafs.Select(m => new { id = m.CorffId, mafs = m}).ToList();
            var corffIdForMaf = context.Corffs
                  .Where(c => System.Data.Entity.DbFunctions.AddYears(c.CorffSubmittedDateTime, 1).Value > dateToCompare)
                  .OrderByDescending(c => c.CorffSubmittedDateTime).Select(c => new { id = c.Id, corff = c}).ToList();
            var intersectResult = from p in corffIdsPerMaf
                          join f in corffIdForMaf on p.id equals f.id
                          select new SelectedCorffData() { CorffId = p.id, ReportNumber = f.corff.ReportNumber, CorffSubmittedDateTime = f.corff.CorffSubmittedDateTime };

            Dictionary<string, SelectedCorffData> dataSelectedForDeletion = intersectResult.GroupBy(x => x.ReportNumber, y => y).ToDictionary(x => x.Key, y => y.FirstOrDefault());
        }
    }
    public class Context
    {
        public List<cMafs> Mafs { get; set;}
        public List<cCorffs> Corffs { get; set;}
    }
    public class cMafs
    {
        public int CorffId { get; set; }
    }
    public class cCorffs
    {
        public DateTime CorffSubmittedDateTime { get; set; }
        public int Id { get; set; }
        public string ReportNumber { get; set; }
    }
    public class Test
    {
    }
    public class SelectedCorffData
    {
        public long CorffId { get; set; }
        public string ReportNumber { get; set; }
        public DateTime CorffSubmittedDateTime { get; set; }
    }
}

最新更新