簡體   English   中英

如何使用select方法中的實例名稱為類實例成員賦值

[英]How to assign value to class instance members using the instance name in a select method

我有這個方法:

    public DemographicData GetDemographicByZipCode(string zipcode)
    {
        DemographicData demoData = new DemographicData();

        using(var context = new DataContext())
        {
            var result = from item in context.Demographic
                         where item.ZipCode == zipcode
                         select item;

            foreach (var data in result)
            {
                demoData.City = data.City;
                demoData.State = data.State;
                demoData.Zip = data.ZipCode;
            }
        }

        return demoData;
    }

我試圖編寫沒有循環的方法,如下所示,但很明顯,它不起作用,因為我不能在表達式樹中使用賦值運算符。

    public DemographicData GetDemographicByZipCode(string zipcode)
    {
        DemographicData demoData = null;
        // Instantiate to new instance in the select method.
        // I need to use this instance demoData

        using(var context = new DataContext())
        {
            var result = from item in context.Demographic
                         where item.ZipCode == zipcode

                         select new DemographicData()
                         {
                             //assign data to instance member here.
                         };
        }

        return demoData;
    }

不,你做不到。 但是,如果您的目標是使demoData代表查詢中的單個結果,那么您可以執行以下操作:

public DemographicData GetDemographicByZipCode(string zipcode)
{
    DemographicData demoData = null;

    using(var context = new DataContext())
    {
        demoData = (from item in context.Demographic
                   where item.ZipCode == zipcode
                   select new DemographicData()
                   {
                       Zip = item.ZipCode,
                       City = item.City,
                       State = item.State
                   }).FirstOrDefault();
    }

    //Do other stuff to demoData here, if needed

    return demoData;
}

它使用FirstOrDefault獲取列表中的第一個(如果沒有則為null)。 在您的示例的循環中,您只是覆蓋了值,因此我假設您只期望一個結果。

更新:如果您期望多個結果,則返回IEnumerable<DemographicData> ,如下所示:

public IEnumerable<DemographicData> GetDemographicByZipCode(string zipcode)
{
    List<DemographicData> demoData = null;

    using(var context = new DataContext())
    {
        demoData = (from item in context.Demographic
                   where item.ZipCode == zipcode
                   select new DemographicData()
                   {
                       Zip = item.ZipCode,
                       City = item.City,
                       State = item.State
                   }).ToList();
    }

    //Do other stuff to demoData here, if needed

    return demoData;
}

在方法中使用List<DemographicData>ToList()來強制它在那里實際執行查詢。 如果您不使用ToList() ,它將在首次訪問列表時執行查詢,當您處置上下文時,該列表將在using之外。 它可能還會抱怨多個枚舉,具體取決於您的代碼。

嘗試:

  var result = from item in context.Demographic
                     where item.ZipCode == zipcode
                     select item;

這會奏效。

City = item.City,
State = item.State,
Zip = item.ZipCode

您的代碼仍然存在的問題是您返回單個DemographicData對象,而結果將是DemographicData對象的集合,即使只有一個滿足條件tem.ZipCode == zipcode

如果您期望只有一個DemographicData實例以這種方式執行

var result = (from item in context.Demographic
                     .
                     (your query here)
                     .
                 ).Single();

如果你期望一個或零,那么用FirstOrDefault()替換Single() FirstOrDefault() 如果您期望一個集合,那么該方法的返回類型應該是IEnumerable<DemographicData>IQueryable<DemographicData>或任何其他最適合您的方法。 如果需要list / array,則將Single()替換為ToList() / ToArray()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM