C# 如何在select方法中使用实例名称为类实例成员赋值

C# 如何在select方法中使用实例名称为类实例成员赋值,c#,linq,C#,Linq,我有这个方法: public DemographicData GetDemographicByZipCode(string zipcode) { DemographicData demoData = new DemographicData(); using(var context = new DataContext()) { var result = from item in context.Demogra

我有这个方法:

    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;
}
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;
}
使用
FirstOrDefault
获取列表中的第一个(如果没有,则为null)。在示例中的循环中,您只是覆盖了这些值,因此我假设您只期望一个结果

更新:如果希望得到多个结果,则返回
IEnumerable
,如下所示:

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;
}
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;
}
public IEnumerable GetDemographicByZipCode(字符串zipcode)
{
列表数据=null;
使用(var context=new DataContext())
{
demoData=(来自上下文中的项。人口统计)
其中item.ZipCode==ZipCode
选择新的人口统计数据()
{
Zip=item.ZipCode,
城市=项目。城市,
状态=项。状态
}).ToList();
}
//如果需要,请在此处执行其他解调操作
返回解调数据;
}
在方法中使用
List
ToList()
,强制它在那里实际执行查询。如果不使用
ToList()
,它将在第一次访问列表时执行查询,当您的上下文被释放时,该查询将位于使用
之外。它还可能会抱怨多次枚举,具体取决于您的代码。

这将起作用

City = item.City,
State = item.State,
Zip = item.ZipCode
代码仍然存在的问题是,返回单个
DemographicData
对象,而结果将是
DemographicData
对象的集合,即使只有一个对象满足条件
tem.ZipCode==ZipCode

如果您只需要一个
demographicsdata
实例,请这样做

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

如果预期为1或0,则将
Single()
替换为
FirstOrDefault()
。如果您需要一个集合,那么该方法的返回类型应该是
IEnumerable
IQueryable
或任何其他最适合您的方法。如果需要列表/数组,则将
Single()
替换为
ToList()
/
ToArray()

分配
解调数据的方式没有任何意义。循环中有一个单实例,并且一遍又一遍地分配它?它只取循环中的最后一个值。您需要多少
demoData
对象?您只需要一个还是多个?我需要使用此demoData实例。为什么?在查询之前您在做什么,而在查询之后您不能做什么?(我更新了我的答案,以明确您可以在查询后对
demoData
执行其他操作)我知道您要去哪里,但返回了多个结果,因此FirstOrDefault将只返回一个结果集。对,但您的代码只返回一个结果集。每次通过循环时都覆盖相同的值<代码>解调数据
是单个对象,而不是列表。那么你希望返回什么呢?我更新了我的答案,以显示如何返回所有结果的列表。