如何在LINQ结果上添加额外的结果 [英] How to add extra results on LINQ result

查看:76
本文介绍了如何在LINQ结果上添加额外的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个列表 EList OList 。从联合中,将不同的结果导入 var b



如何操纵 var b 中的结果以产生具有额外字段的新结果以指示微粒数据行来自EList或OList。谢谢。



预期的最终结果(字段安排)应为以下格式:

ID,姓名,电子邮件,IS_Elist,IS_Olist

1,a1,b1,True,True

2,a2,b2,True,False

3,aa2,bb2 False,True


I have 2 list EList and OList. From the union, get the distinct results into var b.

how do i manipulate the result in var b to produce new result that have an extra field to indicate that the particulate row of data is from EList or OList. thanks.

The expected end result (Field arangement) should be something this format:
ID, Name, Email, IS_Elist, IS_Olist
1, a1, b1, True, True
2, a2, b2, True, False
3, aa2, bb2 False, True

List<EmployeeInfo> EList = new List<EmployeeInfo>();
EList.Add(new EmployeeInfo(1, "a1", "b1"));
EList.Add(new EmployeeInfo(2, "a2", "b2"));

List<EmployeeInfo> OList = new List<EmployeeInfo>();
OList.Add(new EmployeeInfo(1, "aa1", "bb1"));
OList.Add(new EmployeeInfo(3, "aa2", "bb2"));

var b = EList.Union(OList)
        .GroupBy(g => new { g.ID, g.Name, g.Email})
        .Select(s => new EmployeeInfo
        {
            ID = s.Key.ID,
            Name = s.Key.Name,
            Email = s.Key.Email
        });

推荐答案



试试这个,

Hi,
Try this,
List<employeeinfo> EList = new List<employeeinfo>();
EList.Add(new EmployeeInfo(1, "a1", "b1"));
EList.Add(new EmployeeInfo(2, "a2", "b2"));
 
List<employeeinfo> OList = new List<employeeinfo>();
OList.Add(new EmployeeInfo(1, "aa1", "bb1"));
OList.Add(new EmployeeInfo(3, "aa2", "bb2"));

var result = (
    from e in EList
    select new { 
      Id = e.ID, 
      Name = e.Name,
      Email = e.Email,
      IS_Elist = true, 
      IS_Olist = false }
  ).Union(
    from o in OList 
    select new { 
      Id = o.ID, 
      Name = o.Name, 
      Email = o.Email,
      IS_Elist = false, 
      IS_Olist = true }
  ).ToList();





在这里你必须用5列创建新的列表,并且必须在第1列中放入IS_Olist false在第二个查询中查询和IS_Elist为false。



我希望这会有所帮助。

谢谢:)



Here you have to create new list with 5 column and have to put IS_Olist false in first query and IS_Elist false in second query.

I hope this will help.
Thanks :)


这篇关于如何在LINQ结果上添加额外的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆