简体   繁体   English

显示记录外键的标题

[英]Display the title of the record foreign key

Good day! 美好的一天! Faced a problem, there is a table,4 fields (Id,Parent Id,Title,Created, Description) where Parent Id is the foreign key to the Id in the same table. 面对问题,有一个表,其中包含4个字段(标识,父标​​识,标题,已创建,描述),其中父标识是同一表中ID的外键。 How on the client side to display in the table is not ParentId namely the parent record Title, if any? 如何在客户端显示的表中不是ParentId,即父记录Title(如果有)?

Class: 类:

public class MyClass
    {
        public int Id { get; set; }
        public int? ParentId { get; set; }
        public string Title { get; set; }
        public DateTime Created { get; set; }
        public string Description { get; set; }
    }

Controller: 控制器:

        public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {

            Repository repository = new Repository();

            ViewBag.CurrentSort = sortOrder;

            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "Date desc" : "Date";

            if (Request.HttpMethod == "GET")
            {
                searchString = currentFilter;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = searchString;

            var records = from s in repository.GetMyClass()  select s;

            if (!String.IsNullOrEmpty(searchString))
            {
                records = records.Where(s => s.Title.ToUpper().Contains(searchString.ToUpper()));
            }



            switch (sortOrder)
            {
                case "Name desc":
                    records = records.OrderByDescending(s => s.Title);
                    break;
                case "Date":
                    records = records.OrderBy(s => s.Created);
                    break;
                case "Date desc":
                    records = records.OrderByDescending(s => s.Created);
                    break;
                default:
                    records = records.OrderBy(s => s.Title);
                    break;
            }

            int pageSize = 4;
            int pageIndex = (page ?? 1);



            return View("_TableRecords",records.ToPagedList(pageIndex, pageSize));
        }

You need to implement a Self Join. 您需要实现自我连接。

First you need to add one more field to your ViewModel: 首先,您需要在ViewModel中再添加一个字段:

public string ParentTitle { get; set; }

And then change the way you get your data, it should be something like that: 然后更改获取数据的方式,应该是这样的:

IEnumerable<MyClass> myData = 
    from m in t.YourEntity
    join e1 in t.YourEntity on m.ParentId equals e1.Id
    select new MyClass
    { 
        Id = m.Id,
        ParentId = m.ParentId,
        Title = m.Title,
        ParentTitle = e1.Title,
        Created = m.Created,
        Description = m.Description
    };

    return View(myData.ToList());

Try this, selecting all data including parent title in your sql script, like this: 尝试此操作,在sql脚本中选择包括父标题在内的所有数据,如下所示:

DECLARE @DemoTable TABLE(
Id INT,
ParentId INT,
Title NVARCHAR(50),
Created DATETIME,
Description NVARCHAR(50))

INSERT INTO @DemoTable VALUES(1, NULL, 'Fruit', '2018-03-06 17:00:00.000', '')
INSERT INTO @DemoTable VALUES(2, 1, 'Apple', '2018-03-06 17:00:00.000', '')
INSERT INTO @DemoTable VALUES(3, 1, 'Banana', '2018-03-06 17:00:00.000', '')
INSERT INTO @DemoTable VALUES(4, NULL, 'Sport', '2018-03-06 17:00:00.000', '')
INSERT INTO @DemoTable VALUES(5, 4, 'Baseball', '2018-03-06 17:00:00.000', '')
INSERT INTO @DemoTable VALUES(6, 4, 'Football', '2018-03-06 17:00:00.000', '')


SELECT T1.Id, T1.Title, T1.ParentId, T2.Title AS 'ParentTitle'
FROM @DemoTable AS T1
LEFT JOIN @DemoTable AS T2
ON T1.ParentId = T2.Id

Result of execution 执行结果
在此处输入图片说明

You can get all data including parent title 您可以获得包括父标题在内的所有数据
And then, change your viewmodel to this: 然后,将您的viewmodel更改为此:

public class MyClass
    {
        public int Id { get; set; }
        public int? ParentId { get; set; }
        public string ParentTitle { get; set; }
        public string Title { get; set; }
        public DateTime Created { get; set; }
        public string Description { get; set; }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM