简体   繁体   English

如何在 LINQ 中加入表并在 MVC 中返回列表

[英]How to join table in LINQ and return list in MVC

I have this webservice method to get a list of records based on the prefixSA that I pass in from my controller.我有这个 webservice 方法根据我从我的控制器传入的 prefixSA 获取记录列表。

But I don't want to display a guid.但我不想显示 guid。 In another table BizV the name for each guid are stored.在另一个表 BizV 中,存储了每个 guid 的名称。 As the image below.如下图。 在此处输入图片说明

How do I change the code so that from the listing it will get the shortdesc and pass back tolist();如何更改代码,以便从列表中获取 shortdesc 并传回tolist();

[WebMethod]
    public List<ca_ConfigiDeal> Get_iDealConfig(String caller, Guid? prefixSA, Guid? codeCalendar, bool? iscash, bool? isfreeMM, bool? isonContract, Guid? product, Guid? requestType, out ReturnStatus returnStatus, out String errorMessage)
    {
        List<ca_ConfigiDeal> list = null;
        DateTime _startTime = DateTime.Now;
        String _spName = "Get_iDealConfig";
        Object _parameters = new { caller, prefixSA, codeCalendar, iscash, isfreeMM, isonContract,product, requestType};

        returnStatus = ReturnStatus.Unspecified;
        _customMessage = String.Empty;
        ERGODataContext _context = null;

        try
        {
            _context = get_DBContext2();
            DataLoadOptions dataLoad = new DataLoadOptions();
            dataLoad.LoadWith<ca_ConfigiDeal>(x => x.ref_PrefixSA);
            dataLoad.LoadWith<ca_ConfigiDeal>(x => x.ref_CodeCalendar);
            dataLoad.LoadWith<ca_ConfigiDeal>(x => x.ref_Product);
            dataLoad.LoadWith<ca_ConfigiDeal>(x => x.ref_RequestType);

            var _list = _context.ca_ConfigiDeals.Where(x => x.isDeleted == false);
            if (prefixSA.HasValue)
                _list = _list.Where(x => x.PrefixSA == prefixSA);
            list = _list.ToList();
            if (codeCalendar.HasValue)
                _list = _list.Where(x => x.CodeCalendar == codeCalendar);
            list = _list.ToList();
            if (iscash != null)
                _list = _list.Where(x => x.isCashnCarry == iscash);
            list = _list.ToList();
            if (isfreeMM != null)
                _list = _list.Where(x => x.isFreeMM == isfreeMM);
            list = _list.ToList();
            if (isonContract != null)
                _list = _list.Where(x => x.isOnContract == isonContract);
            list = _list.ToList();
            if (product.HasValue)
                _list = _list.Where(x => x.Product == product);
            list = _list.ToList();
            if (requestType.HasValue)
                _list = _list.Where(x => x.RequestType == requestType);
            list = _list.ToList();


            if (list.Count == 0)
            {
                returnStatus = ReturnStatus.NoRecord;
            }
            else
                returnStatus = ReturnStatus.Success;

        }
        catch (Exception ex)
        {
            handleExcept(ref returnStatus, caller, ex, _spName, _parameters, _startTime);
            _customMessage = ex.Message;
        }
        finally
        {
            errorMessage = String.Format("{0}: {1}.{2}", (Int32)returnStatus, returnStatus.ToString(),
                String.IsNullOrEmpty(_customMessage) ? String.Empty : String.Format(" {0}.", _customMessage));
            traceEnd(_context, caller, _spName, _parameters, _startTime, returnStatus, errorMessage);
        }

        return list;
    }

ca_idealconfig table ca_idealconfig 表在此处输入图片说明

a is pointing to ca_ConfigiDeals, b=> BizV table a 指向 ca_ConfigiDeals,b=> BizV 表

        var join = _context.ca_ConfigiDeals.Join(_context.BizV, a=>a.guid, b=>b.rguid, 
    (a,b)=>new{
          PrefixSA = a. PrefixSA,
          guid = a.guid, 
          calendercode = a.calendercode,
          product = a.product,
          request = a.request,
          ShortDesc = b.ShortDesc
          }).Where(x => x.PrefixSA == prefixSA);

after join u can play with like a normal model加入后你可以像普通模特一样玩耍

Edit:: I'm misunderstanding编辑:我误解了

This answer is not be a best practice but it will work这个答案不是最佳实践,但它会起作用

var new_list = new List<Object>()
    foreach(var item in _list){
       var temp = new {
            PrefixSA = _context.BizV.Find(item.PrefixSA).ShortDesc;
            CodeCalendar = _context.BizV.Find(item.CodeCalendar).ShortDesc;
            Product = _context.BizV.Find(item.Product).ShortDesc;
            RequestType = _context.BizV.Find(item.RequestType).ShortDesc;
        }
        new_list.Add(temp);
    }

Edit Aagin ::编辑阿金::

    [WebMethod]
        public List<object> Get_iDealConfig(String caller, Guid? prefixSA,out ReturnStatus returnStatus, out String errorMessage)
        {
            var list = new List<object>();
            DateTime _startTime = DateTime.Now;
            String _spName = "Get_iDealConfig";
            Object _parameters = new { caller, prefixSA };

            returnStatus = ReturnStatus.Unspecified;
            _customMessage = String.Empty;
            ERGODataContext _context = null;

         try
            {
                _context = get_DBContext2();
                var _list = _context.ca_ConfigiDeals.Where(x => x.isDeleted == false);
                if (prefixSA.HasValue)
                     _list = _list.Where(x => x.PrefixSA == prefixSA);
                foreach(var item in _list){
                   var temp = new {
                        PrefixSA = _context.BizV.Find(item.PrefixSA).ShortDes,
                        CodeCalendar = _context.BizV.Find(item.CodeCalendar).ShortDesc,
                        Product = _context.BizV.Find(item.Product).ShortDesc,
                        RequestType = _context.BizV.Find(item.RequestType).ShortDesc
                    };
                list.Add(temp);
                };


                 if (list.Count == 0)
                    {
                        returnStatus = ReturnStatus.NoRecord;
                    }
                    else
                        returnStatus = ReturnStatus.Success;

                }
            catch (Exception ex)
            {
                handleExcept(ref returnStatus, caller, ex, _spName, _parameters, _startTime);
                _customMessage = ex.Message;
            }
            finally
            {
                 errorMessage = String.Format("{0}: {1}.{2}", (Int32)returnStatus, returnStatus.ToString(),
                 String.IsNullOrEmpty(_customMessage) ? String.Empty : String.Format(" {0}.", _customMessage));
                 traceEnd(_context, caller, _spName, _parameters, _startTime, returnStatus, errorMessage);
            }

        return list;

your model for those Guid datatype must change to string because you are getting its string?您的那些Guid 数据类型的模型必须更改为字符串,因为您正在获取它的字符串? and you forgot to loadoptions你忘了加载选项

_context.LoadOptions = dataLoad;

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

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