gpt4 book ai didi

c# - 声明并初始化变量以分配匿名类型列表

转载 作者:行者123 更新时间:2023-12-05 07:07:36 25 4
gpt4 key购买 nike

我尝试连接两个表:

var data = from request in context.Requests
join account in context.AutolineAccts
on request.PkRequest.ToString() equals account.AccountCode
select new
{
ID = request.PkRequest.ToString(),
Location = request.FkLocation,
RequestDate = request.RequestDate.Value,
Requestor = request.FkRequestor,
DebitorNr = request.FkDebitor.ToString(),
NewDebit = request.Debit.ToString(),
ApprovalStatus = request.ApprovalStatus.ToString(),
RecommendationStatus = request.RecommendationStatus.ToString(),
DebitorName = account.CustomerSname,
Limit = account.CreditLimit
};

现在我想根据用户的状态过滤结果集:

// Accounting user
if (ActiveDirectoryHelper.CheckIfUserIsInADGroup(userLogin, AdGroups.ACCOUNTING) )

req = data.Where(x => x.RecommendationStatus == null).ToList();

// After sales manager
else if (ActiveDirectoryHelper.CheckIfUserIsInADGroup(userLogin, AdGroups.SAV_LEADERS))
req = data.OrderByDescending(x => x.ID).ToList();

// Everybody else
else
req = data.OrderByDescending(x => x.PkRequest).ToList();

这就是我被困的地方。当我没有连接并且只检索“请求”类型时,我可以只声明一个请求列表

List<Requests> req;

但是对于 Requests 和 AutolineAccts 的组合,我将不得不声明并初始化一个“项目”列表 (req) 以将结果集分配给 if-else 段。但是我不知道那个匿名变量应该是什么样子。

稍后我必须将结果集映射到我的 IndexViewModels 列表:

foreach (var item in req)
viewModel.CLModels.Add(new IndexViewModel
{
ID = item .PkRequest.ToString(),
Location = item .FkLocation,
RequestDate = item .RequestDate.Value,
Requestor = item .FkRequestor,
DebitorNr = item .FkDebitor.ToString(),
NewDebit = item .Debit.ToString(),
ApprovalStatus = item .ApprovalStatus.ToString(),
RecommendationStatus = item .RecommendationStatus.ToString(),
DebitorName = item.CustomerSname,
Limit = item.CreditLimit
});

有解决这个问题的想法吗?

最佳答案

IEnumerable<dynamic> result = from request in requests
join account in accounts
on request.id equals account.id
select new {id=request.id, name=account.name};

保留访问器,可以进一步查询并作为方法的返回类型。

enter image description here

关于c# - 声明并初始化变量以分配匿名类型列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62082621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com