如何在LINQ中连接表并在MVC中返回列表

时间:2015-11-30 06:41:45

标签: c# asp.net-mvc linq asp.net-mvc-4

我有这个webservice方法来获取基于我从控制器传入的prefixSA的记录列表。

但我不想展示guid。在另一个表BizV中,存储了每个guid的名称。如下图所示。 enter image description here

如何更改代码,以便从列表中获取短片并传回 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表 enter image description here

2 个答案:

答案 0 :(得分:1)

a指向ca_ConfigiDeals,b =&gt; 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);
加入后你可以像普通模特一样玩

编辑::我误会了

这个答案不是最佳做法,但它会起作用

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);
    }

编辑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;

答案 1 :(得分:1)

Guid数据类型的模型必须更改为字符串,因为您正在获取其字符串?而你忘了加载选项

_context.LoadOptions = dataLoad;