Linq-合并两个使用相同方法的语句

时间:2019-04-30 12:12:02

标签: c# linq foreach

我正在尝试将以下代码重写并集成到单个语句中:

因此不必这样做:

     var dbList = await _context.ProfileSections.Select(p => new
        {
            p.Id,
            p.Description,
            p.InformationText
        }).ToListAsync();

        var localisedList = dbList.Select(p => new
        {
            p.Id,
            Description = p.Description.Translate(),
            Information = p.InformationText.Translate()
        }).ToList();

我假设我可以强力键入模型并使用foreach,例如:

 var dbList = await _context.ProfileSections.Select(p => new ProfileSectionModel
        {
            p.Id,
            p.Description,
            p.InformationText
        }).ToListAsync();

        var localisedList = dbList.ForEach(l => {
            l.Description = l.Description.Translate();
            l.InformationText = l.InformationText.Translate();
        }).ToList();

但是随后出现错误消息:Operator '.' cannot be applied to operand of type 'void'

那么将这两个语句组合起来会更容易吗?还是我总是必须在第一次返回匿名类型时使用。不确定处理此问题的正确方法是什么?

编辑

可以使用以下内容,但是EF使用的动态代理出现了问题。我猜这就是为什么要选择匿名类型吗?

    var dbList = await _context.ProfileSections.ToListAsync();

    foreach (var item in dbList)
    {
        item.Description = item.Description.Translate(),
        item.Information = item.InformationText.Translate()
    }

3 个答案:

答案 0 :(得分:0)

完全不使用ForEach。您可以在LINQ Select语句中一次性完成。至于您的错误:我想它来自dbList.ForEach,这是不正确的用法。

void Main()
{
    var listOne = new List<dings>{
        new dings() {id = 1, name = "aaa"},
        new dings() {id = 2, name = "bbb"},
        new dings() {id = 3, name = "ccc"},
    };

    var listTwo = listOne.Select(x => new bums() 
        {
            idbums = x.id + 33, 
            namebums = x.name + "hoho"
        }
    ).ToList();
}

public class dings{
    public int id;
    public string name;
}

public class bums
{
    public int idbums;
    public string namebums;
}

答案 1 :(得分:0)

使用这样的联接:

    class Program
    {
        static void Main(string[] args)
        {
            DataBase _context = new DataBase();

            List<ProfileSections> localisedList = new List<ProfileSections>();

            var results = localisedList  
                           .Select(p => new
                           {
                               Id = p.Id,
                               Description = p.Description,
                               InformationText = p.InformationText,
                               TranslateDescription = p.TranslateDescription,
                               TranslateInformationText = p.TranslateInformationText
                           }).ToList();
        }
    }
    public class DataBase
    {
        public List<ProfileSections> ProfileSections { get; set; }
    }
    public class ProfileSections
    {
        public int Id { get; set; }
        public string Description { get; set; }
        public string InformationText { get; set; }

        public string TranslateInformationText
        {
            get { return TranslateFunction(InformationText); }
            set { ;}
        }

        public string TranslateDescription
        {
            get { return TranslateFunction(Description); }
            set { ;}
        }

        private string TranslateFunction(string s)
        {
            return s;
        }
    }

答案 2 :(得分:0)

由于您的转换方法在LINQ查询中不起作用,因此foreach可能确实有用。尝试这样:

void Main()
{
    var listOne = new List<dings>{
        new dings() {id = 1, name = "aaa"},
        new dings() {id = 2, name = "bbb"},
        new dings() {id = 3, name = "ccc"},
    };

    var listTwo = new List<bums>();
    foreach (var item in listOne)
    {
        listTwo.Add(
        new bums()
            {
                idbums = item.id + 33,
                namebums = item.name + "hoho"
            }
        );
    }
}

public class dings
{
    public int id;
    public string name;
}

public class bums
{
    public int idbums;
    public string namebums;
}