总结Linq或者lamdba的写法(2)

 

 //取符合条件的第一个值:FirstOrDefault()
 var orderSn = _DataBase.OrderHead.Where(p => p.OrderSn.ToLower() == reqdata.Body.OrderSN.ToLower() &&  p.AppId == appid).ToList().FirstOrDefault();
//从数据库中取配置值
var ary = new string[]
                {
                        "aa:appid",
                        "aa:appkey",
                        "aa:socancel:url"                                
                };
var settings = _DataBase.BaseSetting.Where(p => p.AppId == $"{reqdata.Body.whse}" && ary.Contains(p.KeyName))
               .ToDictionary(l => l.KeyName, l => l.KeyValue);//这是结果
var settings = _Database.BaseSetting.Where(l => l.AppId == $"{_WareHouse}" && (l.KeyName == "KeyName" || l.KeyName == "KeyName2" || l.KeyName == "KeyName3")).ToList();
//两种写法 string appid = settings["aa:appid"]; string appid= settings.Where(l => l.KeyName == "aa:appid").FirstOrDefault().KeyValue; //排序 //倒序取出最新的一个记录bid(OrderByDescending:降序) var bid = _Database.inventory.Where(p => p.Whse == whse).OrderByDescending(l => l.BatchId).Take(1).FirstOrDefault(); var invs = _Database.inventory.Where(p => p.BatchId == bid.BatchId && p.Whse == whse && p.SyncStatus == 0).OrderBy(l => l.CreateDateTime).ToList();
关于linq的降序升序这个文档将的很好

Linq排序方式与Lambda排序方式比较以及OrderBy、ThenBy的使用

 

//LINQ TO XML
private
JObject GenerateDelivery(Guid headid, string erpno, string ordersn, string source) { var j = new JArray(( from sns in Database.SerialNumber.Where(l => l.PkEcorderHead == headid) join o in ECDatabase.orderHead.Where(l => l.PkEcorderHead == headid) on sns.PkEcorderHead equals o.PkEcorderHead join d in ECDatabase.OmsEcorderDelivery.Where(l => l.PkEcorderHead == headid) on sns.PkEcorderHead equals d.PkEcorderHead select new { DeliveryDateTime = d.DeliveryDateTime.ToString("yyyy-MM-dd hh:mm:ss"), DeliveryNumber = d.DeliveryNumber, SerialNumber = sns.SerialNumber, Sku = sns.Sku, }
)
.Distinct().ToList().Select
(l
=> new JObject( new JProperty("@tracking_time", l.DeliveryDateTime), new JProperty("@tracking_number", l.DeliveryNumber), new JProperty("@serial_number", l.SerialNumber), new JProperty("@part_no", l.Sku), ))); var rss = new JObject { {"?xml",new JObject { {"@version","1.0" }, {"@encoding","utf-8"} } }, { "order", new JObject { {"@CO", source }, {"sns", new JObject { {"sn", j } } } } } }; return rss; }

 


//LINQ TO JSON
var detail = _Database.orderDetail.Where(l => l.PkEcorderHead == _HeadId).ToList();
if (detail.Count() == 0)
    throw new Exception($"AddSOToWMS:单身错误, 笔数为0");
var jary = new JArray(detail.Select(l => new JObject
                {
                    new JProperty("lineno", l.LineNum.ToString()),
                    new JProperty("goodsname", l.GoodsName),
                    new JProperty("goodsnumber", l.GoodsNumber),
                    new JProperty("partno", l.PartNo)
                }));
var obj = new JObject
                {
                    {"head", new JObject
                        {
                            { "transmessage","" },
                            { "transcode",TransCode },
                            { "transid", TransId },
                            { "errorcode", "" }
                        }
                    },
                    {"body", new JObject
                        {                            
                            { "invoicetitle", head.InvoiceTitle },
                            { "detail", jary} 
                        }
                    }
                };

 

posted @ 2020-11-23 10:27  ProZkb  阅读(146)  评论(0编辑  收藏  举报