简体   繁体   English

通过LINQ查询选择新类,如何转移到另一个模型?

[英]New class selected by LINQ query, how to transfer to another model?

I'm really not understanding this as I've only dabbled in MVC and C#. 我真的不了解这一点,因为我只涉猎MVC和C#。 I apologize if my terminology is wrong or confusing, I will do my best to answer questions. 如果术语错误或令人困惑,我深表歉意,我将尽力回答问题。 I have a couple models like so: 我有几个这样的模型:

 public class DataSharingModels
{
    public string ReferenceID { get; set; }
    public NBTC NBTCGroup { get; set; }
    public Contractors ContractorsGroup { get; set; }
    public Coordinators CoordinatorsGroup { get; set; }
    public NGO NGOGroup { get; set; }
    public Public PublicGroup { get; set; }
    public SelectList FA_RA_List { get; set; }

}

public class NBTC
{
    public String NBTC_FA_Centroid { get; set; }
    public String NBTC_FA_Bound { get; set; }
    public String NBTC_RA_Centroid { get; set; }
    //more properties...
 }

The DataSharingModels class contains the public NBTC NBTCGroup property. DataSharingModels类包含public NBTC NBTCGroup属性。 It is not public List<NBTC> NBTCGroup because there will only be one produced per instance of the controller being hit. 它不是public List<NBTC> NBTCGroup因为被命中的控制器的每个实例只会产生一个。

Now in my controller, I have a LINQ statement that selects a new NBTC class: 现在在我的控制器中,我有一个LINQ语句,该语句选择一个新的NBTC类:

var nbtcVals = (from ds in db.SharingPermissions
                        where ds.FocalRefID.ToString() == ReferenceID
                        && ds.ShareGroup == "NBTC"
                        select new NBTC
                        {
                            NBTC_FA_Centroid = ds.CIP_FA_Centroid,
                            NBTC_FA_Bound = ds.CIP_FA_Boundary,
                            NBTC_RA_Centroid = ds.CIP_RA_Centroid,
                            //more properties...
                        });

Where I'm going wrong is I would like to add that to my DataSharingModels model. 我要出错的地方是要将其添加到DataSharingModels模型中。 I thought the nbtcVals type would be NBTC, but it's IQueryable<##.Models.NBTC>. 我以为nbtcVals类型是NBTC,但它是IQueryable <##。Models.NBTC>。 I understand I could do this, but it seems redundant: 我知道我可以这样做,但似乎多余:

        DataSharingModels dsm = new DataSharingModels();

        if (nbtcVals.Any())
        {
            foreach (var i in nbtcVals)
            {
                dsm.NBTCGroup.NBTC_FA_Centroid = i.NBTC_FA_Centroid;
                dsm.NBTCGroup.NBTC_FA_Boundary = i.NBTC_FA_Bound;
                dsm.NBTCGroup.NBTC_RA_Centroid = i.NBTC_RA_Centroid;
                //more properties...
            }
        }

What is a more direct way to do this? 有什么更直接的方法可以做到这一点? There must be one. 必须有一个。 I supposed I could also return an anonymous type in the LINQ query and then assign each property in the foreach like dsm.NBTCGroup.NBTC_RA_Centroid = i.NBTC_RA_Centroid but that seems the same as the other way. 我以为我也可以在LINQ查询中返回匿名类型,然后在dsm.NBTCGroup.NBTC_RA_Centroid = i.NBTC_RA_Centroid类的foreach分配每个属性,但这似乎与其他方法相同。

var nbtcgroup = (from ds in db.SharingPermissions
                        where ds.FocalRefID.ToString() == ReferenceID
                        && ds.ShareGroup == "NBTC"
                        select new NBTC
                        {
                            NBTC_FA_Centroid = ds.CIP_FA_Centroid,
                            NBTC_FA_Bound = ds.CIP_FA_Boundary,
                            NBTC_RA_Centroid = ds.CIP_RA_Centroid,
                            //more properties...
                        })
              .OrderByDescending(n => n.Id) // or some other property that could identify sorting
              .FirstOrDefault();

This one has a translation to SQL (LIMIT or TOP depending on backend). 这有一个到SQL的转换(LIMIT或TOP,取决于后端)。

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

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