在某些集合中添加多种不同类型的记录并对其进行排序

时间:2015-04-22 12:57:56

标签: c# linq asp.net-mvc-5 json.net

我想以一种排序的格式发送两个不同集合的集合作为Json。我无法用Linq做到这一点。需要一些帮助。还有更好的方法。谢谢!

这是我的行动。

    public ActionResult GetAllJobPosts()
    {
        var Institutes = new List<Institute>()
        {
            new Institute(){InstituteId="ins1",InstituteName="name1",Location="Mumbai"},
            new Institute(){InstituteId="ins2",InstituteName="name2",Location="Navi Mumbai"},
            new Institute(){InstituteId="ins3",InstituteName="name3",Location="Thiruvananthpuram"}
        };

        var Companys = new List<Company>()
        {
            new Company(){CompanyId="com1",CompanyName="comName1",Location="Mumbai"},
            new Company(){CompanyId="com2",CompanyName="comName2",Location="Navi Mumbai"},
            new Company(){CompanyId="com3",CompanyName="comName3",Location="Mumbai"}
        };

        var Organizations = new List<Organization>() 
        {
            new Organization(){OrganizationId="org1",OrganizationName="orgName1",Location="Navi Mumbai"},
            new Organization(){OrganizationId="org2",OrganizationName="orgName2",Location="Navi Mumbai"},
            new Organization(){OrganizationId="org3",OrganizationName="orgName3",Location="Mumbai"},
        };
        var CompanyJobPosts = new List<CompanyJobPost>()
        {
            new CompanyJobPost(){CompanyId="com1",CompanyJobPostId="com1jp1",CreatedOn=System.DateTime.Now.AddDays(-2),JobDiscription="Tester",KeySkils="Sylanium"},
            new CompanyJobPost(){CompanyId="com1",CompanyJobPostId="com1jp2",CreatedOn=System.DateTime.Now.AddDays(-3),JobDiscription="Developer",KeySkils="C#"},
            new CompanyJobPost(){CompanyId="com2",CompanyJobPostId="com2jp1",CreatedOn=System.DateTime.Now.AddDays(-5),JobDiscription="Tester",KeySkils="Sylanium"},
            new CompanyJobPost(){CompanyId="com2",CompanyJobPostId="com2jp2",CreatedOn=System.DateTime.Now.AddDays(-6),JobDiscription="Developer",KeySkils="C#"},
            new CompanyJobPost(){CompanyId="com3",CompanyJobPostId="com3jp1",CreatedOn=System.DateTime.Now.AddDays(-7),JobDiscription="Tester",KeySkils="Sylanium"},
            new CompanyJobPost(){CompanyId="com3",CompanyJobPostId="com3jp2",CreatedOn=System.DateTime.Now.AddDays(-8),JobDiscription="Developer",KeySkils="C#"}
        };
        var InstituteJobPosts = new List<InstituteJobPost>() 
        {
            new InstituteJobPost(){InstituteId="ins1",InstituteJobPostId="ins1jp1",CreatedOn=System.DateTime.Now.AddDays(-1),JobDiscription="Trainer",KeySkils="C#",ExtraField="MDifferent"},
            new InstituteJobPost(){InstituteId="ins1",InstituteJobPostId="ins1jp2",CreatedOn=System.DateTime.Now.AddDays(-8),JobDiscription="Developer",KeySkils="Java",ExtraField="MDifferent"},
            new InstituteJobPost(){InstituteId="ins2",InstituteJobPostId="ins2jp1",CreatedOn=System.DateTime.Now.AddDays(-1),JobDiscription="Trainer",KeySkils="Java",ExtraField="MDifferent"},
            new InstituteJobPost(){InstituteId="ins2",InstituteJobPostId="ins2jp2",CreatedOn=System.DateTime.Now.AddDays(-8),JobDiscription="Developer",KeySkils=".Net",ExtraField="MDifferent"},
            new InstituteJobPost(){InstituteId="ins3",InstituteJobPostId="ins3jp1",CreatedOn=System.DateTime.Now.AddDays(-1),JobDiscription="Trainer",KeySkils="C#",ExtraField="MDifferent"},
            new InstituteJobPost(){InstituteId="ins3",InstituteJobPostId="ins3jp2",CreatedOn=System.DateTime.Now.AddDays(-8),JobDiscription="Developer",KeySkils="Java",ExtraField="MDifferent"}
        };
        var allJobPosts=new List<object>();
        foreach (var item in CompanyJobPosts)
        {
            allJobPosts.Add(new { JType = "Company", JobPost = item });
        } 
        foreach (var item in InstituteJobPosts)
        {
            allJobPosts.Add(new { JType = "Institute", JobPost = item });
        }
        //var allJobPostsOrderdByDate=??
        return Json(allJobPosts, JsonRequestBehavior.AllowGet);

    }

以下是我的模型,只是为了简化。

namespace WebApplication1.Models
{
public class Company
{
    public string CompanyId { get; set; }
    public string CompanyName { get; set; }
    public string Location { get; set; }
}
public class Organization
{
    public string OrganizationId { get; set; }
    public string OrganizationName { get; set; }
    public string Location { get; set; }
}
public class Institute
{
    public string InstituteId { get; set; }
    public string InstituteName { get; set; }
    public string Location { get; set; }
}
public class CompanyJobPost
{
    public string CompanyJobPostId { get; set; }
    public string CompanyId { get; set; }
    public string KeySkils { get; set; }
    public string JobDiscription { get; set; }
    public DateTime CreatedOn { get; set; }
}
public class OrganizationJobPost
{
    public string OrganizationJobPostId { get; set; }
    public string OrganizationId { get; set; }
    public string KeySkils { get; set; }
    public string JobDiscription { get; set; }
    public DateTime CreatedOn { get; set; }
    public string ExtraField2 { get; set; }
}
public class InstituteJobPost
{
    public string InstituteJobPostId { get; set; }
    public string InstituteId { get; set; }
    public string KeySkils { get; set; }
    public string JobDiscription { get; set; }
    public DateTime CreatedOn { get; set; }
    public string ExtraField { get; set; }
}

}

最后我的甜蜜观点

<input name="GetAllJobPosts" id="GetAllJobPosts" type="submit" value="Search Jobs">
<ul id="JobPostList"></ul>

<script src="~/Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">
$("#GetAllJobPosts").click(function () {
    var actionUrl = '@Url.Action("GetAllJobPosts", "Default")';
    $.getJSON(actionUrl, displayDetailData);
});

function displayDetailData(response) {
    if (response != null) {

        for (var i = 0; i < response.length; i++) {
            $("#JobPostList").append("<li>" + response[i].JType + " " + (response[i].JobPost).CreatedOn + "</li>")
        }
    }
}

谢谢!

1 个答案:

答案 0 :(得分:1)

根据您的评论,我只能假设以下内容,因此我会尽力指出正确的方向:

两者之间没有共同的继承对象 - 也就是说,您希望在属性 x 上对它们进行排序,但属性 x 未定义为存在于两者中。

那么,解决方案?简单:在两个具有您希望排序的属性之间添加一个公共接口,类或抽象类,然后按它排序:

public interface IJobPost
{
    DateTime CreatedOn { get; set; }
}

然后修改三个现有对象:

public class CompanyJobPost : IJobPost
{
    public string CompanyJobPostId { get; set; }
    public string CompanyId { get; set; }
    public string KeySkils { get; set; }
    public string JobDiscription { get; set; }
    public DateTime CreatedOn { get; set; }
}
public class OrganizationJobPost : IJobPost
{
    public string OrganizationJobPostId { get; set; }
    public string OrganizationId { get; set; }
    public string KeySkils { get; set; }
    public string JobDiscription { get; set; }
    public DateTime CreatedOn { get; set; }
    public string ExtraField2 { get; set; }
}
public class InstituteJobPost : IJobPost
{
    public string InstituteJobPostId { get; set; }
    public string InstituteId { get; set; }
    public string KeySkils { get; set; }
    public string JobDiscription { get; set; }
    public DateTime CreatedOn { get; set; }
    public string ExtraField { get; set; }
}

最后,行动:

var allJobPosts=new List<IJobPost>();
// Add other posts to allJobPosts here.
var allJobPostsOrderdByDate = allJobPosts.OrderBy(x => x.CreatedOn).ToList();

注意:代码未经测试。 LINQ查询可能有效,也可能无效。这一切都来自记忆。

编辑:您还可以在三者之间共享您希望强制执行的任何其他属性。这就是界面抽象类的用途。这也意味着您可以共享描述或其他属性。